0
votes

MYSQL query:

$sql_product = "SELECT * FROM products WHERE product_id= $product_id";
        $sql_result = $dbh->query($sql_product);

PHP:

    foreach($sql_result as $row)
        {
        $product_id=$row['product_id'];
        $product_name=$row['product_name'];
        $price=$row['price'];
        $picture=$row['picture'];

        // output
        echo "
        <div>$product_name</div>
        <div>$price</div>
        <div>
            <form action='cart.php' method='POST' id='buy'> 
                Amount: <select name='amount'>
                         <option value='1'>1</option>
                         <option value='2'>2</option>
                         <option value='3'>3</option>
                    </select>                   

            </form>
        </div>
        ";
        if (isset($_POST['amount']))
            {
            $amount = $_POST['amount'];
            echo $amount, '<br /><br />';
            }
        }
    echo "<button type='submit'class='btn btn-primary' form='buy'>Buy</button>";

I have a problem with the option tag in the PHP foreach loop. It's a shopping cart where there is an HTML option tag inside a form. The value from the option tag is then made into a variable called $amount. But whenever I press the button the amount changed for all the products in the shopping cart. So if I have 3 products in the cart and I want to buy 1 of the first and 2 of the second and third, then the variable $amount changes to 1 for all products. Can anyone help me out?

2

2 Answers

0
votes

You are assigning the same id (buy) to all the forms you are creating inside the loop and there should be only one id per page. Do something like:

<form action='cart.php' method='POST' id='buy_$row@index'>

and put the button inside the loop, with the correct for id:

echo "<button type='submit'class='btn btn-primary' form='buy_$row@index'>Buy</button>";
0
votes

add a counter or something in the for loop.

something like

$x=0;
foreach() {

     //your code here

     <form action='cart.php' method='POST' id='buy'> 
            Amount: <select name='amount'".$x.">
                     <option value='1'>1</option>
                     <option value='2'>2</option>
                     <option value='3'>3</option>
                </select>                   

        </form>

      //your code here

      $x++;

}

instead of creating $x you could try with $row. you just have to add something to the name='amount'".$variable."'>

if you would change your foreach

 foreach($sql_result as $row)

to

 foreach($sql_result as $k => $row)

you could use that $k

`name='amount'".$k."'>`



////* UPDATE *////

as sayd in the comments the problem could be that the form gets created in the foreach. So I recommend to create it outside. This is how you could do it. There are a few changes, try to find them all.

// output is only $html at the end //
$html = '<form action="cart.php" method="POST" id="buy">';
foreach($sql_result as $row) {
    $product_id=$row['product_id'];
    $product_name=$row['product_name'];
    $price=$row['price'];
    $picture=$row['picture'];

    $html.='
        <div>$product_name</div>
        <div>$price</div>
        <div>

                Amount: <select name="amount'.$row.'">
                         <option value="1">1</option>
                         <option value="2">2</option>
                         <option value="3">3</option>
                    </select>
        </div>';
        if (isset($_POST['amount'])) {
            $amount = $_POST['amount'];
            $html.= $amount.'<br /><br />'; //here string concatennation in php is with .
        }
}
$html.='<button type="submit" class="btn btn-primary" form="buy">Buy</button>';
$html.='</form>';

// output happends here
echo $html;

// now all of this above you could wrap in a function and then
// return $html;