如何使用php脚本更新多行
我正在将数据插入php脚本,但我不知道如何使用php脚本更新多行 我的代码格式是:如何使用php脚本更新多行,php,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(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);
?>
您需要在循环中包含查询执行。不是在它之后:
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());
}
如果您已经在使用MySQLi,我强烈建议您研究如何使用参数化查询,而不是将值直接嵌入SQL,这样可以更快地执行多次(就像您希望的那样),并且可以防止SQL注入攻击。
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);
?>