gpt4 book ai didi

php - 在php中删除网格中的行时出错

转载 作者:行者123 更新时间:2023-11-29 13:52:12 25 4
gpt4 key购买 nike

我已经以网格格式显示了一些数据,现在我试图通过单击最后一行中的删除按钮来删除该行,但出现错误。有人可以告诉我我犯了什么错误吗?我的代码:

<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');

?>

我收到以下错误:

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/38/11053638/html/admin/deleteQuote.php on line 6
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/content/38/11053638/html/admin/deleteQuote.php on line 6 DELETE FROM quotes WHERE quoteid = Warning: Cannot modify header information - headers already sent by (output started at /home/content/38/11053638/html/admin/deleteQuote.php:6) in /home/content/38/11053638/html/admin/deleteQuote.php on line 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']);

但是,由于 mysqli 支持准备好的语句,因此您应该使用它而不是连接字符串。那么你就不必担心转义字符串。

关于php - 在php中删除网格中的行时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16510251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com