0

I am implementing a shopping website for food items. I have a cart for each of my visitor and in the cart, the details of their food items are fed. I have a field called Food_Name in my cart table and I have another table called food where I have Food_Name, Food_Id among other details.

I am trying to retrieve the food id by using Food_Name in my cart table and performing a query to get the corresponding food id in my food table. However, I am not getting any result for my food item. The query however works when I hardcode the value of one of my food name for e.g 'Vegetable Gratin' in the place of $foodCommand

Please help me. It's very important

Here is part of the code

$retrieveCart="SELECT * FROM cart WHERE cartNo='$cartNo'";
$res3=$conn->query($retrieveCart);

while($rowresult=$res3->fetch(PDO::FETCH_ASSOC))
{

    $foodCommand=$rowresult['Food_Name'];

    echo $foodCommand; //I get the correct food name

    $retrievefid = "SELECT Food_Id FROM food WHERE Food_Name='$foodCommand'";
    echo $retrievefid;

    echo "</br>";

    $foodresult = $conn->query($retrievefid);
    $roww= $foodresult->fetch(PDO::FETCH_ASSOC);
    $fid = $roww['Food_Id'];
    echo "fid is ".$fid;
    $qty=$rowresult['qty'];
    $fprice=$rowresult['Food_Price'];

    $odetails="INSERT INTO 
                                visitor_order_details(Food_Id,OV_Id,O_Qty,`Price_Paid(Rs)`) 
                        VALUES('$fid','$vid','$qty','$fprice')";

    $conn->exec($odetails);

}//endwhile 

Edit:

I am getting the following error:

Notice: Trying to access array offset on value of type bool in C:\xampp\htdocs\trial2\checkout.php on line 140

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Hi, use multi_query() instead of query() function – giovybus Apr 25 '20 at 12:13
  • @giovybus They're using PDO, not mysqli_. – Funk Forty Niner Apr 25 '20 at 12:24
  • @giovybus what difference does it make? –  Apr 25 '20 at 12:24
  • Any errors via PDO? https://www.php.net/manual/en/pdo.error-handling.php and error reporting? – Funk Forty Niner Apr 25 '20 at 12:27
  • @FunkFortyNiner ya it did not work –  Apr 25 '20 at 12:27
  • PDF vs MYSQLI https://websitebeaver.com/php-pdo-vs-mysqli – giovybus Apr 25 '20 at 12:28
  • @FunkFortyNiner got this error Notice: Trying to access array offset on value of type bool in C:\xampp\htdocs\trial2\checkout.php on line 140 –  Apr 25 '20 at 12:29
  • @FunkFortyNiner getting this error because it is not getting the food id value –  Apr 25 '20 at 12:32
  • I guess `$roww` is `false` because of some error, so `$roww['Food_Id']` throws an error. – Roman Hocke Apr 25 '20 at 12:32
  • @RomanHocke i echoed the values. I got $foodCommand correctly and when i hardcode the value of $foodCommand like 'Vegetable Gratin', I get the food id –  Apr 25 '20 at 12:34
  • Did you check for errors on the query also with the first link I gave? That doesn't check PHP errors, it checks for PDO errors. Also, what do you get when you `var_dump()` the query where you're retrieving from the `food` table? – Funk Forty Niner Apr 25 '20 at 12:42
  • @FunkFortyNiner yes i did and i did not get additional errors. How should i use the var_dump()? Is it on the query or the results? –  Apr 25 '20 at 12:47
  • You use `var_dump($variable)` and replace the variable with the ones associated with the ones you're using for the `food` table query. Have a look also at the duplicate that another member used to close the question. If that still didn't work, then ping that person to see if they can shed some light on that and possibly ask them reopen if it didn't solve your question. – Funk Forty Niner Apr 25 '20 at 12:49
  • @FunkFortyNiner I get this string(67) "SELECT Food_Id FROM food WHERE Food_Name='Vegetable Gratin '" –  Apr 25 '20 at 12:51
  • `WHERE Food_Name='Vegetable Gratin '"` < notice you have a trailing space in there at the end, most likely the reason. Try using `trim()` maybe, or find out why the space. Maybe it was created with `\n` somewhere. – Funk Forty Niner Apr 25 '20 at 12:51
  • 1
    @FunkFortyNiner THANKS A LOT!!! I've been on that for 5 hours! U r a genius :D –  Apr 25 '20 at 12:56
  • Right on, glad to hear it :D *Cheers!* Funny the little things like that and what they'll do sometimes. Hey.... I've been there too ;-) Take care. – Funk Forty Niner Apr 25 '20 at 12:58
  • On a side note, you must use JOIN in your SQL and prepared statements in PDO – Your Common Sense Apr 25 '20 at 13:10
  • 1
    @FunkFortyNiner yes we know that well :D.. Cheers u too n stay safe! ;) –  Apr 25 '20 at 14:38

0 Answers0