PHP批量更新$ _POST值

时间:2015-10-28 11:29:32

标签: php mysql batch-updates

我有一个$ _POST参数如下:

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

SQL用于在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 />';

}

然而它的输出:

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'

以下面的形式更新查询的正确方法是什么?

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 个答案:

答案 0 :(得分:1)

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

    // query here..

    $i++;
}

更好的方法是以这种格式发送数据:

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

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

)

您可以重命名表单的输入:

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]">

这样你就可以用它来循环:

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

答案 1 :(得分:1)

试试这个

<?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++;
}
?>

答案 2 :(得分: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 />';   

}

答案 3 :(得分:0)

最好像这样使用数组;

$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 />';


    }