Hi guys,
New to both this forum and to php in general so any help is very much appreciated! My college project depends on it
I am following what seems to be a fairly common php tutorial online and I have come across several errors which I have managed to fix but I have been stuck for the best part of 2 days on this issue and I cannot for the life of me figure out why it is not working. The issue revolved around the addtobasket function working when a user is logged in but not working when he is logged out.
if(isset($_SESSION['SESS_LOGGEDIN'])) { $sql = "INSERT INTO orders(customer_id,registered, date) VALUES(". $_SESSION['SESS_USERID'] . ", 1, NOW())"; mysqli_query($db, $sql) or die(mysql_error()); $_SESSION['SESS_ORDERNUM'] = mysql_insert_id(); $itemsql = "INSERT INTO orderitems(order_id, product_id, quantity) VALUES(". $_SESSION['SESS_ORDERNUM']. ", " . $_GET['id'] . ", ". $_POST['amountBox'] . ")"; mysqli_query($db, $itemsql) or die(mysql_error()); }
In the above code, when the user is logged in everything works fine. The ORDERS table and ORDERITEMS table are both written to as expected.
However when a user is not logged in the ORDERITEMS table is not written to properly.
else { $sql = "INSERT INTO orders(registered, date, session) VALUES(". "0, NOW(), '" . session_id() . "')"; mysqli_query($db, $sql) or die(mysql_error()); $_SESSION['SESS_ORDERNUM'] = mysql_insert_id(); $itemsql = "INSERT INTO orderitems(order_id, product_id, quantity) VALUES(". $_SESSION['SESS_ORDERNUM'] . ", " . $_GET['id'] . ", ". $_POST['amountBox'] . ")"; mysqli_query($db, $itemsql) or die(mysql_error()); }
I think the problem is with the $_SESSION['SESS_ORDERNUM'] = mysql_insert_id(); part of the query but I could be wrong. In this scenario, the query to the ORDERS table is fine, however the ORDERITEMS table does not pick up the order_id from the ORDERS table and leads to errors when trying to view the basket - i.e. it just tells me that there are no items added to the basket.
I've attached the full php file. Any help much appreciated.
addtobasket.php