Hi, I’m having problems working out how to update a MySQL table based on some images for a record and whether they exist or not.
Basically in my edit screen I have some fields, some of which are for images of the product. The user can change the image using a file / browse form input, if the image exists, or they can also add new ones.
I used this code successfully back when using the old MySQL query:
$query = "UPDATE forsale SET Price = '$price', PropertyType = '$propertytype', Bedrooms = '$bedrooms', Location = '$location', Status = '$status', Description = '$apostrophefix_description', OverseasYN = '$overseas', ArchivedYN = '$archived'";
if ($image1) { $query .= ", Image1 = '$image1'"; }
if ($image2) { $query .= ", Image2 = '$image2'"; }
if ($image3) { $query .= ", Image3 = '$image3'"; }
if ($image4) { $query .= ", Image4 = '$image4'"; }
if ($image5) { $query .= ", Image5 = '$image5'"; }
if ($image6) { $query .= ", Image6 = '$image6'"; }
if ($image7) { $query .= ", Image7 = '$image7'"; }
$query .= " WHERE `PropertyID` = '$id'";
}
But because I’m now using mysqli and the syntax has to include brackets for it work, I can’t find a way of incrementing the query based on whether or not a certain field from the form has a value.
For example:
$query = $mysqli->query("UPDATE forsale SET Price = '$price', PropertyType = '$propertytype', Bedrooms = '$bedrooms', Location = '$location', Status = '$status', Description = '$apostrophefix_description', OverseasYN = '$overseas', ArchivedYN = '$archived'");
If I then try to use if ($image1) {$query .= “, Image1 = ‘$image1’”; } etc. etc. it doesn’t work.
I can’t simply assign all the values from the form inputs, e.g set Image1, Image2, Image3 etc. to have the values of $image1, $image2, $image3 and so on, because the user might have not changed the photos- so the empty value would overwrite the value already in the database.
Any ideas? Thanks.