我有一个表格,显示我网站页面上的用户ID和用户名。我试图使用表的最后一列中的删除按钮从SQL表中删除行。我确实遵循了here的一些指示,但我似乎遇到了问题。我收到这个错误:
mysqli_real_escape_string()需要2个参数,1个给定
我似乎无法解决这个问题。谁能解释我做错了什么?
$sql = "SELECT id, username FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table classs><tr><th>ID</th><th>Name</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"]. "</td>";
echo "<td>" . $row["username"]. "</td>";
echo "<td align='right'><a href='delete.php?did=".$row['id']."'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
这是我的delete.php
require_once'config.php';
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_GET['did'])) {
$delete_id = mysqli_real_escape_string($_GET['did']);
$sql = ("DELETE FROM users WHERE id = '".$delete_id."'");
if($sql) {
echo "<br/><br/><span>deleted successfully...!!</span>";
} else {
echo "ERROR";
}
}
答案 0 :(得分:0)
尝试通过此
更改您的delete.php文件require_once'config.php';
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_GET['did'])) {
//Escape the id using the function intval (Only if is a integer)
$delete_id = intval($_GET['did']);
//Use the method query of $conn
$sql = $conn->query("DELETE FROM users WHERE id = '".$delete_id."'");
if($sql) {
echo "<br/><br/><span>deleted successfully...!!</span>";
} else {
echo "ERROR";
}
}