douhui8025 2015-05-03 00:34
浏览 60
已采纳

删除基于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.

  • 写回答

1条回答 默认 最新

  • drgdn82648 2015-05-03 00:40
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0