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

时间:2013-05-12 17:38:53

标签: php mysql forms

我以网格格式显示了一些数据,现在我试图在最后一行点击删除按钮时删除该行,但是我收到错误。有人可以告诉我我犯了什么错误。我的代码:

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

&GT;

我收到以下错误:

  

警告:mysql_real_escape_string()[function.mysql-real-escape-string]:无法通过/ home / content中的套接字'/var/lib/mysql/mysql.sock'(2)连接到本地MySQL服务器第3行的/38/11053638/html/admin/deleteQuote.php
  警告:mysql_real_escape_string()[function.mysql-real-escape-string]:无法在第6行的/home/content/38/11053638/html/admin/deleteQuote.php中建立到服务器的链接   删除引号WHERE quoteid =   警告:无法修改标头信息 - 已在/ home / content / 38/11053638 / html / admin / deleteQuote中发送的标头(在/home/content/38/11053638/html/admin/deleteQuote.php:6中开始输出)。第11行的php

1 个答案:

答案 0 :(得分:1)

如果您尚未打开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支持预处理语句,因此您应该使用它而不是连接字符串。然后你不必担心转义字符串。