06-19-2009, 02:48 AM
|
#1 (permalink)
|
|
The Contributor
Join Date: Oct 2008
Posts: 75
Thanks: 4
|
Grabbing IDs from Database
Hey
I'm creating a simple shopping cart but I'm stuck trying to figure out why I'm not getting the output I'm looking for.
PHP Code:
function product_exists($input_id)
{
$mysqli = new mysqli('localhost', 'root', '', 'store');
$result = $mysqli->query("SELECT ID FROM products");
$array = array();
while ($row = $result->fetch_object())
{
$array[] = $row->ID;
}
foreach ($array as $product_id)
{
if ($product_id == $input_id)
return true;
else
return false;
}
}
Basically what I'm trying to do is store all the IDs that are in my database into an array and compare the ID the user has given me. If it matches it needs to return true, else false.
Here is the front side:
PHP Code:
$product_id = $_REQUEST['id'];
if (product_exists($product_id))
echo 'Product Exist!';
else
echo 'Not working yet';
If I do add.php?id=1 it works fine. But if I do add.php?id=2 it will not work.
Can anyone lend a hand please?
|
|
|
|