1

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!

3
  • echo your delete query to check that are you getting code or not??? Commented Jan 24, 2015 at 11:04
  • Is coursecode a string or a numeric value? Commented Jan 24, 2015 at 11:06
  • It is a string e.g. "C800" or "F568", stored as a VARCHAR in the database. Was this one of the reasons it was going wrong? Commented Jan 24, 2015 at 14:31

1 Answer 1

2

Surround it with single quotes like this:

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

$query = "DELETE FROM course WHERE coursecode='{$coursecode}'";
Sign up to request clarification or add additional context in comments.

3 Comments

I would upvote but I don't have enough reputation yet! The second version worked :-) Thank you for your reply! I wonder why this worked as it was on one example and not on the other... The mysteries of PHP...
If using mysql, it is deprecated as of PHP 5.5.0. If you are using it don't forget to escape variables to avoid sql injection with mysql_real_escape_string($coursecode). Better to learn mysqli or PDO moving forward.
Ah okay. I am using PHP 5.6.17 now but the examples we were given probably used an older version... Also, in the examples the variables were numbers, where as mine used letters aswell. Possibly string/int thing going on there. And yes, we learnt about mysql_real_escape_string recently :-) And also magic quote so I should figure those out in time. Also, thank you again! I changed the format of how I referenced $coursecode to the first format you suggested in other areas of my code and it fixed all the other issues I was having! :D You're a star!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.