2

I got a $_POST params as below:

Array ( [qty-1] => 1 [uniprice-1] => 22.00 [qty-2] => 2 [uniprice-2] => 12.00 ) 

SQL use to update in db:

foreach($_POST as $key => $value) {

    $q  = "UPDATE `table` SET ";
    $q .= "stock=stock + '".$value."', ";
    $q .= "price='".$value."' ";
    $q .= "WHERE fid='".$fid."' AND vid='".$key."'";

    echo $q.'<br />';

}

However its output:

UPDATE `stock` SET stock=stock + '1', price='1' WHERE fid='9' AND vid='1'
UPDATE `stock` SET stock=stock + '22.00', price='22.00' WHERE fid='9' AND vid='1'
UPDATE `stock` SET stock=stock + '2', price='2' WHERE fid='9' AND vid='2'
UPDATE `stock` SET stock=stock + '12.00', price='12.00' WHERE fid='9' AND vid='2'

what is the proper way to update the query in form like below?

UPDATE `stock` SET stock=stock + '1', price='22.00' WHERE fid='9' AND vid='1'
UPDATE `stock` SET stock=stock + '2', price='12.00' WHERE fid='9' AND vid='2'
4
  • Fix the loop, preferably by choosing a saner method to pass in the POST parameters. Commented Oct 28, 2015 at 11:35
  • @Jon, can you show a working example? Commented Oct 28, 2015 at 11:40
  • Add the html form code. Or try to implement input arrays Commented Oct 28, 2015 at 11:42
  • 1
    This is prone to SQL Injection. Additionally you can use the input array syntax to loop over inputs. stackoverflow.com/a/20184680/3714134 Commented Oct 28, 2015 at 11:49

4 Answers 4

1

Try this

<?php
// Input
$_POST['qty-1']=1;$_POST['uniprice-1']=22.00;$_POST['qty-2']=2;$_POST['uniprice-2']=12.00;


$x=0;$fid=9;
foreach($_POST as $key => $value) {


    $q  = "UPDATE `table` SET ";
    $key=str_replace("qty-","",$key);
    $value2=str_replace("uniprice-","",$key);
    $q .= "stock=stock + '".$value2."', ";
    if($x%2!=0)
    {
    $q .= "price='".$value."' ";
    $q .= "WHERE fid='".$fid."' AND vid='".$fkey."'";
    echo $q.'<br />';
    }
    $fkey=$key;
    $x++;
}
?>
Sign up to request clarification or add additional context in comments.

Comments

1
$i = 1;
while(isset($_POST['qty-'.$i]) && isset($_POST['uniprice-'.$i])) {
    $qty = $_POST['qty-'.$i];
    $price = $_POST['uniprice-'.$i];

    // query here..

    $i++;
}

A better approach would be to send the data in this format:

Array
(
    [0] => Array
        (
            [qty] => 1
            [uniprice] => 22
        )

    [1] => Array
        (
            [qty] => 2
            [uniprice] => 12
        )

)

You can renaming the inputs of your form:

qty1: <input type="text" name="data[0][qty]">
price1: <input type="text" name="data[0][uniprice]">

qty2: <input type="text" name="data[1][qty]">
price2: <input type="text" name="data[1][uniprice]">

So that you can loop it with:

foreach ($_POST['data'] as $data) {
    echo 'qty: ', $data['qty'], "\n";
    echo 'uniprice: ', $data['uniprice'], "\n";
}

Comments

0
$test = array( "qty-1" => 1, "uniprice-1" => 22.00, "qty-2" => 2, "uniprice-2" => 12.00 );

///Make 2 Array

$odd = array();
$even = array();
$i=0;
foreach( $test as $key => $value ) {

    if( $i%2==0) { //Even
        $even[] = $value;
    }
    else {
        $odd[] = $value;
    }

    $i++;   
}

//then 

$fid = 9;
foreach($odd as $key => $value) {


    $q  = "UPDATE `table` SET ";
    $q .= "stock=stock + '".$even[$key]."', ";
    $q .= "price='".$value."' ";
    $q .= "WHERE fid='".$fid."' AND vid='".$even[$key]."'"; 
    echo $q.'<br />';   

}

Comments

0

It is better to use array like this ;

$array = Array ( array("qty" => 1, "uniprice" => 22.00) , array("qty" => 2, "uniprice" => 12.00) ) ;

    foreach ($array as $arr) {

        $q  = "UPDATE `table` SET ";
        $q .= "stock=stock + '".$arr['qty']."', ";
        $q .= "price='".$arr['uniprice']."' ";
        $q .= "WHERE fid=9 AND vid='".$arr['qty']."'";

        echo $q.'<br />';


    }

Comments

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.