PHP / MySQL删除记录不会删除

时间:2015-01-24 11:02:19

标签: php html mysql sql-delete

这让我抓狂!我有一个名为课程列表的网页,我使用PHP创建一个HTML表格,列出存储在MySQL数据库中的课程(使用Wampserver)

<?php

require "dbconn.php";

$query = "SELECT coursecode, coursename FROM course";

$results = $connect->query($query);

$numrow = $results->num_rows;
?>

<html>
  <head></head>
    <body>
      <table border="1">

        <?php
        $count = 0;
        while ($count < $numrow)
            {
                $row = $results->fetch_assoc();
                extract($row);

                echo "<tr>";

                echo "<td>";
                echo "<a href='updatecourseform.php?coursecode=".$coursecode."'>".$coursecode."</a>";

                echo "<td>";
                echo $row['coursename'];
                echo "</td>";

                echo "<td>";
                echo "<a href='deletecourse.php?coursecode=".$coursecode."'>Delete</a>";
                echo "</td>";

                echo "</tr>";

                $count = $count + 1;
            }
        ?>

        </table>
        <br />
        The number of courses found was: <?php echo $numrow; ?>
        <br /><br />
        Click <a href="addcourse.html">here</a> if you want to add a Course
    </body>
</html>

这会打印出一个漂亮的HTML表格,其中包含正确的所有数据,右侧的一行带有Delete字样,这是一个超链接,应该允许我删除一个课程。但是当我点击删除时,它不会删除!这是我的deletecourse.php。

<?php

require "dbconn.php";

$coursecode = $_GET['coursecode'];

$query = "DELETE FROM course WHERE coursecode =".$coursecode;

$results = $connect->query($query);

header("Location: courselisting.php");
?>

这是非常令人沮丧的,因为我有一个完全相同的例子为不同的数据库工作...我刚刚关闭了所有的变量名,但基本上逻辑和语法和一切都是一样的!

1 个答案:

答案 0 :(得分:2)

用这样的单引号包围它:

$query = "DELETE FROM course WHERE coursecode='" . $coursecode . "'";

$query = "DELETE FROM course WHERE coursecode='{$coursecode}'";