gerkintrigg Posted September 8, 2009 Share Posted September 8, 2009 I'm trying to work out why the following code doesn't work. I am trying to add a record to a database, then get that same record, use the product_id of the record and redirect the page header to re-load the page to add the new record to a shopping cart session. the code is live at: http://6thanniversary.co.uk/products/1 Once you add the engraving options and click "add to basket" it adds the engraving as a new product in the database and puts the cufflinks in the shopping cart, but it should then get the new product (the engraving) and reload the page to add the engraving to the shopping cart... Can anyone PLEASE help..? Thanks. Code below: if(!empty($_POST['engraving'])){ $engraving=$_POST['engraving']; $q="INSERT INTO `products` ( `Name` , `Price` , `postage` , `category` , `img` , `small_img` , `description` , `discounts` , `keywords` , `engraving_chars` , `product_id` ) VALUES ( 'Engraving for $item: $engraving', '0.00', '0.00', '0', 'customisation/engraving_big.jpg', 'customisation/engraving_small.jpg', 'Engraving with: $engraving', '0', '', '0', NULL );"; mysql_query($q); $q="SELECT * FROM `products` ORDER BY `product_id` DESC LIMIT 1"; $sql=mysql_query($q); $r=mysql_fetch_array($sql); $engraving_id=$r['product_id']; $go='shop_cart_add.php?prod='.$engraving_id; header("Location: ".$go); } Link to comment https://forums.phpfreaks.com/topic/173545-argh-session-arrays-redirects-headache/ Share on other sites More sharing options...
cbolson Posted September 8, 2009 Share Posted September 8, 2009 Hi, you don't actually say exactly in what way it is not working (php error, empty basket, not redirect etc.) I assume your product_id column is a unique auto-increment column. You can save yourself some trouble and code by using mysql_insert_id() after mysql_query($q); to get the id of the item that you have just inserted. You can't trust getting the items in reverse order as somebody else might add an item at the same time and mess up the system. So you could just use this (having inserted the product): $go='shop_cart_add.php?prod='.mysql_insert_id(); header("Location: ".$go); Now, if you find that the page is not reloading, the php function header("location: ....") must be called before anything is output to the browser, including whitespace. Chris Link to comment https://forums.phpfreaks.com/topic/173545-argh-session-arrays-redirects-headache/#findComment-914790 Share on other sites More sharing options...
gerkintrigg Posted September 9, 2009 Author Share Posted September 9, 2009 To clarify, the site adds the product (like the cufflink) to the shopping cart and the engraving to the product database, but it doesn't re-load the shopping cart page to add the engraving to the cart. I tried the mysql_insert_id() but that doesn't work either. It appears to reload the page though... because when i click on "add to basket", it adds the cufflinks as quantity=2... so some kind of reloading appears to be happening... Link to comment https://forums.phpfreaks.com/topic/173545-argh-session-arrays-redirects-headache/#findComment-915286 Share on other sites More sharing options...
cbolson Posted September 9, 2009 Share Posted September 9, 2009 Well, I am not really sure what your problem is. When I go to the url that you posted I can click on the "Add to basket" button and I am taken to the shopping car with the item in it. If I then go to another product and do the same (ie click on the button) I am taken to the shopping cart again, now with the 2 items that I clicked in it. It seems alright to me (apart from the missing images). Is this not what you want to happen? ah, hang on... should the product and the engraving show up as 2 seperate items in the shopping cart? Chris Link to comment https://forums.phpfreaks.com/topic/173545-argh-session-arrays-redirects-headache/#findComment-915290 Share on other sites More sharing options...
gerkintrigg Posted September 9, 2009 Author Share Posted September 9, 2009 No. I want the engraving to appear as a separate product in the shopping cart. Link to comment https://forums.phpfreaks.com/topic/173545-argh-session-arrays-redirects-headache/#findComment-915414 Share on other sites More sharing options...
cbolson Posted September 9, 2009 Share Posted September 9, 2009 From what I can see from your source code (forgive me if I am wrong) you are just adding the product to the cart by a simple href link like this: shop_cart_add.php?prod=1 I can't see anywhere that it is passing the textarea contents to the next page. Why don't you put it in a form (ie the product id and the engraving textarea) so that it can be posted "properly"? Chris Link to comment https://forums.phpfreaks.com/topic/173545-argh-session-arrays-redirects-headache/#findComment-915425 Share on other sites More sharing options...
gerkintrigg Posted September 10, 2009 Author Share Posted September 10, 2009 yeah, i think that's the next stage Link to comment https://forums.phpfreaks.com/topic/173545-argh-session-arrays-redirects-headache/#findComment-916019 Share on other sites More sharing options...
phpretard Posted September 10, 2009 Share Posted September 10, 2009 $go=$engraving_id; header("Location: shop_cart_add.php?prod={$go}"); Link to comment https://forums.phpfreaks.com/topic/173545-argh-session-arrays-redirects-headache/#findComment-916023 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.