Jump to content

ARGH! (Session arrays + redirects = headache)


gerkintrigg

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.