Reputation: 43
i am inserting data into php script is done .but i have not getting how to update mulitiple rows using php script.
my code format is:
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);
?>
Upvotes: 0
Views: 92
Reputation: 12089
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);
?>
Upvotes: 2
Reputation: 219884
You need to include your query execution in your loop. Not after it:
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());
}
Upvotes: 0