How to change one or more selection values? If I enter a value it works. Still, if I enter two values in both input fields, it doesn't work, showing the following error.
Error updating record: There is an error in your SQL syntax; check The manual corresponding to your MariaDB server version Line 1 Syntax used near 'nat = 'saf' WHERE id = '16''
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(); }
@m-eriksson 评论的实施方法: