I am trying to update a SQL table from a PHP array using foreach() .
I noticed that only the last array value is written to all table fields.
here is my Code.
$property_img[]=$image;
if (empty($property_img)){
return null;}
$count=Count($property_img);
if(Count($property_img)===0)
{ var_dump($count);
$property_img=$property_img[0];
$sql2="UPDATE `property_img` set property_img=:property_img where property_img.pro_id=".$id;
}else {
$sql2="UPDATE `property_img`\n set property_img=:property_img\n case";
foreach($property_img as $property_img):
$property_img=$property_img[$count];
$sql2.="where property_img.pro_id=".$id;
endforeach;
return $sql2;
}
$params2=[':property_img'=>$property_img];
$inserting2=$db->runQuery($sql2,$params2);
I’m confused about the apparent use of $property_img as both an array and a scalar variable. If it’s allowed, and I’m not sure it is, it doesn’t seem to be a good choice, unless I’m misinterpreting what I am reading.
Nor does $count inside the loop for that matter:
$property_img=$property_img[$count];
Where does $id come from anyway? And why go to the trouble of using prepared statements for the image, but not for the id?