创建删除php表单
我在尝试用PHP创建一个delete表单以使用给来宾的ID删除数据时遇到了问题。我是这方面的新手 以下是错误:创建删除php表单,php,mysql,Php,Mysql,我在尝试用PHP创建一个delete表单以使用给来宾的ID删除数据时遇到了问题。我是这方面的新手 以下是错误: Error: DELETE FROM MyGuests WHERE id= LIMIT 1 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 'LIMIT 1' at
Error: DELETE FROM MyGuests WHERE id= LIMIT 1
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 'LIMIT 1' at line 1
以下是我的delete.php:
<?php
include_once('mysql.php');
$ins= "DELETE FROM MyGuests WHERE id=$id LIMIT 1";
if ($conn->query($ins) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error: " . $ins . "<br>" . $conn->error;
}
?>
<!DOCTYPE html>
<html>
<body>
<a href='index.php'>Home</a><br>
<h2>Delete User</h2>
<form action="/delete.php" method="post">
Guest ID:<br>
<input type="text" name="id" >
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
删除用户
来宾ID:
作为最简单的修复:
<?php
include_once('mysql.php');
// make sure that form is submitted
if (isset($_POST['id'])) {
$ins = "DELETE FROM MyGuests WHERE id=" . $_POST['id'] . " LIMIT 1";
if ($conn->query($ins) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error: " . $ins . "<br>" . $conn->error;
}
}?>
$id
未定义。您正在提交一个表单,因此它可能是:DELETE FROM MyGuests,其中id={$\u POST['id']}
你也不需要限制1。我想它总是只删除一条记录。您缺少定义$id的实际内容,所以添加$id=。。。请仔细阅读并使用准备好的声明
$ins = "DELETE FROM MyGuests WHERE id=? LIMIT 1";
$st = $conn->prepare($ins);
$st->bind_param('i', $_POST['id']);
if ($st->execute() === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error: " . $ins . "<br>" . $conn->error;
}