我已经在网格格式中显示了一些数据,现在我正试图在最后一行删除按钮的单击上删除行,但我得到错误。有人能告诉我我犯了什么错误吗?我的代码:
<form action="deleteQuote.php" method="post">
<?php
require_once('config.php');
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE)
or die ('Cannot connect to db');
$result = $conn->query("SELECT quoteid,name,quote FROM `quotes`, category WHERE category.catid = quotes.catid and quotes.isactive = 1");
echo "<table>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td><input type='hidden' name='delete_id' value=$row[quoteid] /></td> <td>$row[name]</td><td>$row[quote]</td>
<td><input type='submit' value='Delete' /></td></tr>";
}
echo "</table>";
?>
</form>
deleteQuote.php:
<?php
if(isset($_POST['delete_id']) && !empty($_POST['delete_id'])){
require_once('config.php');
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE)
or die ('Cannot connect to db');
$delete_id = mysql_real_escape_string($_POST['delete_id']);
echo "DELETE FROM quotes WHERE quoteid =".$delete_id;
$result = $conn->query("DELETE FROM quotes WHERE quoteid =".$delete_id);
}
header('Location: quotes.php');
?>
我得到以下错误:
警告:mysql_real_escape_string() [function。MySQL -real-escape-string]:无法通过套接字'/var/lib/MySQL/MySQL连接到本地MySQL服务器。在/home/content/38/11053638/html/admin/deleteQuote.php的第6行中使用sock' (2)
警告:mysql_real_escape_string() [function。mysql-real-escape-string]:无法在/home/content/38/11053638/html/admin/deleteQuote.php中建立到服务器的链接DELETE FROM quotes WHERE quoteid =警告:无法修改报头信息-报头已经由(输出开始于/home/content/38/11053638/html/admin/deletequote .php:6)在/home/content/38/11053638/html/admin/deletequote .php在第11行
如果没有打开mysql连接,则不能使用mysql_real_escape_string()
。你正在使用mysqli而不是mysql,所以你需要使用等效的mysqli方法。
改变:
$delete_id = mysql_real_escape_string($_POST['delete_id']);
:
$delete_id = $conn->real_escape_string($_POST['delete_id']);
然而,由于mysql支持预处理语句,你应该使用预处理语句而不是连接字符串。这样就不用担心转义字符串了