Kittyinsocks
Kittyinsocks

Reputation: 55

PHP/MySQL Delete Record doesn't Delete

This is driving me crazy! I have a webpage called course listing where I am using PHP to create a HTML table listing the courses stored in a MySQL database (using 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>

and this prints out a nice HTML table with all the data correctly in it, and a row on the right with the word Delete which is a hyperlink and should allow me to delete a course. But when I click Delete, it doesn't Delete! Here is my deletecourse.php.

<?php

require "dbconn.php";

$coursecode = $_GET['coursecode'];

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

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

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

This is super frustrating because I have the exact same example working for a different database... I've just switched out all the variable names, but essentially the logic and syntax and everything is the same!

Upvotes: 1

Views: 1186

Answers (1)

Kerry Kobashi
Kerry Kobashi

Reputation: 801

Surround it with single quotes like this:

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

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

Upvotes: 2

Related Questions