为什么这段代码只删除带有一个单词名称的书籍

时间:2016-03-09 22:22:57

标签: php mysqli

由于某种原因,当我调用此deletebook.php时,查询始终为true,但只有在名称中包含一个单词时才会实际删除该书。任何建议我已经相当困难,可以使用一些帮助。      

    $book = $_GET["book"];
    $email = $_SESSION["email"];

    $servername = "";
    $username = "";
    $password = "";
    $dbname = "";

    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    // sql to delete a record
    $sql = "DELETE FROM books WHERE email = '". $email ."' and name = '". $book ."'";

    if ($conn->query($sql) === TRUE) {
        echo "Record deleted successfully";
    } else {
        echo "Error deleting record: " . $conn->error;
    }

    $conn->close();



?>

这是来自managebooks.php的相关代码

while($row = mysql_fetch_array($result)) {

            echo    "<div id='book'>
                <ol>
                <li>"; 
            echo    $row['name'];
            echo    "</li><li>";
            echo    $row['price'];
            echo    "</li>
                </ol>
                <form action='deleteBook.php' method='get'>
                <input type='hidden' name='book' value=". $row['name'] ." />
                <button type='submit'>Delete</button></form></div>";

        }

2 个答案:

答案 0 :(得分:0)

尝试将“value”的值放在引号中。另外,在回显多行html时尝试使用heredoc;它使得引用更容易。 (最佳做法是双引所有HTML属性的所有值。)

答案 1 :(得分:-1)

我认为你应该让你的sql字符串更清洁它可能有帮助

$sql="DELETE FROM books WHERE email='$email' AND name='$book'";

您还可以打印出$ name的值,以实际查看发送到$ sql字符串的内容