Urgent. Unable to insert id

I am a begginer in php. I am working for a school project and have come into a problem. I am guessing that it is simple but i can’t find it. My webstore saves the order in an array on a session. Here is the insert for the products that are bought.

foreach($_SESSION[“shopping_cart”] as $keys => $values)
{
$INSERT2 = “INSERT Into bestilling_has_vare (bestilling_bestilling_id, vare_vare_id, vare_varenavn, kvantum) values(’$last_id’, ‘2’, ‘{$values[“varenavn”]}’, ‘{$values[“kvantum”]}’)”;

mysqli_query($conn, $INSERT2);
}

Here i take the values from the session and insert them into a table. The only way i could get it to insert it propperly is to manyally put in the product id which is 2 (The second value in the insert). When i try to use for example $values[“product_id”], then it doesn’t work. When i echo the other values then they come out fine on the website, but when i do so with the product id then it is blank. I thought i could get the product id by selecting it with the product name, but i dont know how. Does anybody know how i can get the product id or fetch it with the product name?

If i am missing anything just say so and i will add more. I am using mysql as my database. In addition this is a school project so i don’t need to make it secure against sql injection.

Being a student, you have not learned how to debug your code. One trick which helps is to just display the query before processing it to see if it is “well-formed” or not. My guess is that it is not working the way you think it should. So, for testing and debugging the code, add this line between $INSERT2 = and mysqli_query
die($insert2);
Then run the page and when it gets down to the insertion code, the page will fail (or DIE) and show you the actual query itself. It will show you what the query looks like after putting in the variable values. In this way, you can see where the error is. Then, fist the code to make the query correct.
Also, normally, you have one index in each table that is the row id. This is normally auto-increment field and you do NOT include it in the INSERT command. You just let the MySQLi system handle creating it. If you are using real id’s, it might be a problem with that.
Hope this helps…

The OP edited his existing thread for this, here - Need help with Php shop insert id and added the rest of the cart code. I recommend that any further replies take place in that thread.

Sponsor our Newsletter | Privacy Policy | Terms of Service