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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab