php - 更新记录时出错:您的 SQL 语法有误;检查手册
问题描述
这是我的代码,但我有错误
更新记录时出错:您的 SQL 语法有误;检查与您的 MariaDB 服务器版本相对应的手册,以获取正确的语法,以便在第 9 行的 '' 附近使用
<?php
$conn = mysqli_connect("localhost", "root", "", "physical_therapy");
$conn->query("SET NAMES UTF8");
$id = $_POST["id"];
$fn = $_POST["Cus_Name"];
$age = $_POST["Cus_Age"];
$address = $_POST["Cus_Address"];
$phone = $_POST['Cus_Phone'];
$em = $_POST['Cus_Email'];
$pwd = $_POST["Cus_Password"];
$gender = $_POST["Cus_Gender"];
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "UPDATE customer SET
Cus_Name='" . $fn . "',
Cus_Age='" . $age . "',
Cus_Address='" . $address . "',
Cus_Phone='" . $phone . "',
Cus_Password='" . $pwd . "',
Cus_Email='" . $em . "',
Cus_Gender='" . $gender . "'
WHERE Cus_id=" . $id;
if (mysqli_query($conn, $sql)) {
echo "Update Successfully!!<br><br>";
echo "<a href=\"ProfileCus.php\">Go to Home</a>";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
mysqli_close($conn); ?>
解决方案
查询不起作用可能有两个原因:
- 您的数据中有一个
'
正在破坏您的 SQL。 $id
是空的。
您还应该使用准备好的语句来避免 sql 注入:
$conn = mysqli_connect("localhost", "root", "", "physical_therapy");
$conn->query("SET NAMES UTF8");
$id = $_POST["id"];
$fn = $_POST["Cus_Name"];
$age = $_POST["Cus_Age"];
$address = $_POST["Cus_Address"];
$phone = $_POST['Cus_Phone'];
$em = $_POST['Cus_Email'];
$pwd = $_POST["Cus_Password"];
$gender = $_POST["Cus_Gender"];
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = 'UPDATE customer SET Cus_Name = ?, Cus_Age = ?, Cus_Address = ?, Cus_Phone = ?, Cus_Password = ?, Cus_Email = ?, Cus_Gender = ? WHERE Cus_id = ?';
if ($stmt = mysqli_prepare($conn, $sql)) {
$stmt->bind_param('s', $fn);
$stmt->bind_param('i', $age);
$stmt->bind_param('s', $address);
$stmt->bind_param('s', $phone);
$stmt->bind_param('s', $pwd);
$stmt->bind_param('s', $em);
$stmt->bind_param('s', $gender);
$stmt->bind_param('i', $id);
if ($stmt->execute()) {
echo "Update Successfully!!<br><br>";
echo "<a href=\"ProfileCus.php\">Go to Home</a>";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
}
mysqli_close($conn);
推荐阅读
- windows-10 - 当前按住 Alt/Ctrl 时,如何在 Alt+Tab/Ctrl+Tab 之后重新映射键?
- javascript - 如何删除许多文件的所有 # 注释行?
- r - 依赖项“quadprog”不可用于安装包“bfast”
- javascript - window.open() 在第二次点击时没有打开正确的窗口大小
- tcl - 如何强制执行完全字节码编译?
- r - data.table 按组汇总多列
- java - 为什么我在下面的代码中遇到以下错误:线程“main”java.lang.OutOfMemoryError中的异常:Java堆空间
- ruby - 如何使用 FbGraph2 gem 获取与用户关联的帐户/页面?
- apache - 将 QUERY_STRING 传递给类似规则的 RedirectMatch
- matlab - 如何在matlab中绘制几个向量qith quiver3