创建删除 php 表单

我在尝试使用PHP中创建删除表单以使用提供给客人的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 line 1

这是我的删除.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>


慕田峪4524236
浏览 86回答 2
2回答

蝴蝶刀刀

作为最简单的解决方法:<?phpinclude_once('mysql.php');// make sure that form is submitted&nbsp; &nbsp;&nbsp;if (isset($_POST['id'])) {&nbsp; &nbsp; $ins = "DELETE FROM MyGuests WHERE id=" . $_POST['id'] . " LIMIT 1";&nbsp; &nbsp; if ($conn->query($ins) === TRUE) {&nbsp; &nbsp; &nbsp; &nbsp; echo "Record deleted successfully";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; echo "Error: " . $ins . "<br>" . $conn->error;&nbsp; &nbsp; }}?>另请注意,将原始 /值传递到查询文本中是不安全的。由于您没有提到您使用的API(PDO或mysqli),因此我在查询中串联了值,但您不得这样做。尽快移至预准备语句。$_POST$_GET$_POST预准备语句版本(我假设您根据比较使用):mysqli$ins = "DELETE FROM MyGuests WHERE id=? LIMIT 1";$st = $conn->prepare($ins);$st->bind_param('i', $_POST['id']);if ($st->execute() === TRUE) {&nbsp; &nbsp; echo "Record deleted successfully";} else {&nbsp; &nbsp; echo "Error: " . $ins . "<br>" . $conn->error;}

慕工程0101907

$id未定义。您正在提交表单,因此它可能应该是:DELETE FROM MyGuests WHERE id={$_POST['id']}你也不应该需要 LIMIT 1。我想它总是只删除1条记录。
打开App,查看更多内容
随时随地看视频慕课网APP