MySQL不删除记录 [英] MySQL not deleting records
问题描述
我正在尝试获取一种表格来删除表中的行,但是除了行删除之外,我已完成所有工作.它清除表中的所有值,但不删除行或ID.我已经尝试提交了
I am trying to get a form to delete rows in a table, but I am getting everything done but the row deletion. It clears all value in the table but it does not delete the row or ID. I have tried committing it
PHP脚本
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Setting up the Database Connection
$servername = "localhost";
$username = "root";
$password = "Fh3dSWIWCLd4";
$dbname = "servers";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Capturing the POST Data
$id = $_POST['id'];
$sql = "DELETE FROM sv_info WHERE id='$id' LIMIT 1";
// Executing the SQL statement
if ($conn->query($sql) === TRUE) {
header("Location:http://osgaming.joec.pw/login/");
} else {
echo "Error: " . $sql . "
" . $conn->error;
}
// Closing the database connection
$conn->close();
}
?>
HTMLform
<?php include 'sql-dbsv-del.php'; ?>
<h2><b class="e">Delete Entries</b></h2>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" class="form-group">
<label for="id">Database ID</label>
<input type="number" name="id" class="form-control" placeholder="Database ID"><br />
<input type="submit" value="Delete Entry" class="btn btn-danger">
</form>
https://gyazo.com/ca3f10f862b3993f5d716cb49242304b 它对数据库中的变量有什么作用
https://gyazo.com/ca3f10f862b3993f5d716cb49242304b There is an image to what it is doing to the variable in the database
推荐答案
了解基本的PHP字符串语法:
Learn basic PHP string syntax:
$sql = "DELETE FROM sv_info WHERE id='.$id.' LIMIT 1";
^--start of PHP string ^---end of PHP string
您正在生成文字查询字符串
You're generating the literal query string
DELETE FROM sv_info WHERE id='.4.' LIMIT 1
请注意,您对PHP字符串串联的错误尝试实际上是如何成为查询字符串的一部分.您已经 IN 一个php字符串,因此您无法在该字符串中执行PHP-PHP不可递归嵌入/执行.
Note how your bad attempt at PHP string concatenation actually became part of the query string. you're already IN a php string, so you can't execute PHP inside that string - PHP is not recursively embeddable/executable.
其中任何一种都可以工作:
Either of these would work:
$sql = "DELETE FROM sv_info WHERE id='$id' LIMIT 1";
$sql = "DELETE FROM sv_info WHERE id='" . $id . "' LIMIT 1";
但是,当然,仍然会让您容易受到 sql注入攻击.
but of course, still leave you vulnerable to sql injection attacks.
这篇关于MySQL不删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!