PHP批量更新$_POST值



我得到了一个$_POST参数,如下所示:

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

SQL用于在数据库中更新:

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'

试试这个

<?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++;
}
?>
$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";
}
$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 />';   
}

最好使用这样的数组;

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

    }

相关内容

  • 没有找到相关文章

最新更新