删除基于php中varchar主键的行

删除基于php中varchar主键的行

问题描述:

I'm trying to delete a particular row in a table in php and I'm trying to use the primary key which happens to be of the type varchar, the primary key is course_Code

course_Code||course_Title||credit_Load||lecturer_id
  csc 450  ||    S.E     ||    3      ||     0

lecturer_id will always be 0 for all records added because I want to use it as a foreign key, only then would the value be changed from 0 to something else; the problem is when I have only one value in the table and I delete it, the query runs but a new row gets added with only credit_Load and lecturer_id having values of 0 while the other columns remain empty:

course_Code||course_Title||credit_Load||lecturer_id
           ||            ||    0      ||     0

This is the code i've written for firstpage.php, the value in $result['course_Code'] is the course code fetched from the database:

<a href="processor3.php?coursecode=<?php echo $result['course_Code']; ?>

Then processor3.php:

if (isset($_GET['coursecode'])){         
    $course_id = $_GET['coursecode'];
    $query = mysql_query("DELETE FROM courses WHERE course_Code = '$course_id'");
    header("Location:firstpage.php?succ=2");
}

I know I'm using the deprecated way of connecting to mysql in php, but that's not the main issue, I'm trying to teach someone php and he didn't seem to understand pdo bindings very well, so I want us to progress slowly to pdo. I guess I'm a bad teacher.

Terrible, terrible TERRIBLE!!!

 $query = mysql_query("DELETE FROM courses WHERE course_Code = '$course_id'");

Please never teach anybody to query this way. ALL variables should be escaped, as otherwise there is SQL INJECTION vulnerability.

As for your problem, I suspect 2 possible issues:

  1. somewhere without your knowledge a script to add row is called
  2. you are using wrong MySQL viewer to confirm contents of the table after deletion.

Are you sure neither of the above is happening?