0

I am building a shopping basket with various products options. I have created my form with a get action:

<form method="get" action="">
<input name="category" type="hidden" value="<?php echo $category; ?>">
<input name="product_option_id" type="hidden" value="<?php echo $showProductOptions['id']; ?>">
<input type="text" maxlength="3" size="2" value="1" class="formqty" id="quantity_wanted" name="qty">

">

When this is sent the URL looks like this:

menu.php?category=fruit-and-dessert&product_option_id=13&qty=1&product_option_id=14&qty=2&formadd=

What I need help with is to add the product option id and the quantity into a mySQL table. I have created this script but it doesn't add into the database? I don't get any error message either?

if(isset($_GET['formadd'])) {
foreach ($_GET as $productoptionid => $quantity) {
if(is_numeric($productoptionid) && is_numeric($quantity)) {
mysql_query(" INSERT INTO standard_basket SET session_id = '".$sessionid."', product_option_id = '".$productoptionid."', quantity = '".$quantity."'") or die(mysql_error());
}
}
}

Can anyone help? Many thanks!

4
  • 1
    what are you actually Doing ?? Insert or Update ?? Check syntax of query Commented Dec 17, 2013 at 12:46
  • Inserting into the database. Commented Dec 17, 2013 at 12:48
  • @Ashish: The OP's INSERT query is 100% correct. You can have INSERT & SET. I just fell foul of that one too. Commented Dec 17, 2013 at 13:10
  • Yaa.. It later clicked to me Commented Dec 17, 2013 at 13:45

2 Answers 2

1

The issue is your foreach -

foreach ($_GET as $productoptionid => $quantity) {
   if(is_numeric($productoptionid) && is_numeric($quantity)) {

With your given url params, you would have the following values

$productoptionid => product_option_id
$quantity => 13

$productoptionid => qty
$quantity => 1

$productoptionid => product_option_id
$quantity => 14

$productoptionid => qty
$quantity => 2

so since your $productoptionid are strings (and they are not linked to your qty), your INSERT is never executed as is_numeric($productoptionid) is always false.


One way to do this would be to use the product_option_id as the input name array key name="qty[<?php echo $showProductOptions['id']; ?>]"-

<form method="get" action="">
<input name="category" type="hidden" value="<?php echo $category; ?>">
<input type="text" maxlength="3" size="2" value="1" class="formqty" id="quantity_wanted" name="qty[<?php echo $showProductOptions['id']; ?>]">

then your foreach loop could be -

if(isset($_GET['formadd'])) {
    foreach ($_GET['qty'] as $productoptionid => $quantity) {
        if(is_numeric($productoptionid) && is_numeric($quantity)) {
            mysql_query(" INSERT INTO standard_basket SET session_id = '".$sessionid."', product_option_id = '".$productoptionid."', quantity = '".$quantity."'") or die(mysql_error());
        }
     }
}

note: mysql_ is deprecated. Take a look at http://php.net/manual/en/mysqlinfo.api.choosing.php about how to update to mysqli_ or PDO

Sign up to request clarification or add additional context in comments.

Comments

0

Are you sure you get the right values in your loop? Because as the parameter list is, it would give you the product id at the first iteration and in every second the quantity (if qty is quantity...)

2 Comments

I think you have a point. Should the product option ID be a different name every time?
If the parameters are delivered as such, a workaround could be to have two vars outside the loop (q, id) and inside the loop you set them depending on the index - maybe change your var $productoptionid to $varname) and then if ($varname == 'qty') { $q = $quantity; } if ($varname == 'product_option_id') { $id = $quantity; } then every second iteration you store the value to the DB ... hope you get the logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.