如何使用PHP脚本更新多行

时间:2014-06-12 15:28:15

标签: php

我正在将数据插入到php脚本中。但我还没有得到如何使用php脚本更新多行。

我的代码格式是:      

  if(is_array($sheetData)){

        //$sql = "INSERT INTO 13_product_id (product_id,ext_product_id) values ";

        $valuesArr = array();
        $flag=0;
        foreach($sheetData as $row){

            if($flag==0 || $row['E']== 'Not Found on build.com'){

                $flag =1;

                continue;

            }
            //print_r($row);
            $product_id =$row['A'];
            $ext_product_id=$row['D'];


           // $valuesArr[] = "('$product_id','$ext_product_id')";

        }
         $sql = "UPDATE 13_product_id SET ext_product_id='$ext_product_id[$row]' WHERE product_id='$product_id[$row]'";
    //$sql .= implode(',', $valuesArr);
        print_r($sql);
       //mysqli_query($con,$sql) or exit(mysqli_connect_errno()); 
    }

    mysqli_close($con);
    ?>

2 个答案:

答案 0 :(得分:2)

if(is_array($sheetData)){

    //$sql = "INSERT INTO 13_product_id (ext_product_id,product_id) values (?, ?)";
    $sql = "UPDATE 13_product_id SET ext_product_id= ? WHERE product_id= ?";
    $stmt = mysqli_prepare($con, $sql);
    mysqli_stmt_bind_param($stmt, "ss", $ext_product_id, $product_id);

    $flag=0;
    foreach($sheetData as $row){

        if($flag==0 || $row['E']== 'Not Found on build.com'){
            $flag =1;
            continue;
        }

        $product_id = $row['A'];
        $ext_product_id = $row['D'];
        mysqli_stmt_execute($stmt);

    }
}

mysqli_close($con);
?>

答案 1 :(得分:0)

您需要在循环中包含查询执行。不是在它之后:

foreach($sheetData as $row){
    if($flag==0 || $row['E']== 'Not Found on build.com'){
        $flag =1;
        continue;
    }
    $product_id =$row['A'];
    $ext_product_id=$row['D'];

    $sql = "UPDATE 13_product_id SET ext_product_id='$ext_product_id[$row]' WHERE product_id='$product_id[$row]'";
    mysqli_query($con,$sql) or exit(mysqli_errno()); 
 }