How can I change one value or more values of choice? If I enter a single value, it works. Still, if I enter two values in two input fields, it doesn't work, showing me the following error.
Error updating record: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'nat = 'saf' WHERE id = '16'' at line 1
if (isset($_POST['modifica'])) {
$id = $_POST['id'];
$semaphore = false;
$sql = "UPDATE users SET ";
$fields = array('nume', 'nat', 'email', 'telefon');
foreach ($fields as $field) {
if (isset($_POST[$field]) and !empty($_POST[$field])) {
$var = ($_POST[$field]);
$sql .= $field." = '$var'";
$semaphore = true;
}
}
if ($semaphore) {
$sql .= " WHERE id = '$id'";
($sql);
}
if ($conn->query($sql) === true) {
echo "Record updated successfully";
} else {
echo "Error updating record: ".$conn->error;
}
$conn->close();
}