如何更改一个或多个选择值?如果我输入一个值,它就会起作用。尽管如此,如果我在两个输入字段中输入两个值,它不起作用,显示以下错误。
更新记录时出错:您的 SQL 语法有错误;检查 与您的 MariaDB 服务器版本相对应的手册 第 1 行 '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(); }
P粉7980104412024-03-21 00:02:31
@m-eriksson 评论的实施方法:
$sql = "UPDATE users SET nume = :nume, nat = :nat, email = :email, telefon = :telefon"; $fields = array('nume', 'nat', 'email', 'telefon'); if(count($fields) > 0 ){ $this->update($sql, $fields, $con) $semaphore = true; } public function update ($sql, $fields, $con) { $update = $con->prepare($query); return $update->execute($fields); }