使用PHP删除SQL记录

时间:2016-06-17 06:38:27

标签: php mysql sql-server

我是PHP和SQL的初学者。我一直在尝试使用以下代码删除SQL表中的行,但它不起作用。请帮忙。

<?php
/* 
 DELETE.PHP
 Deletes a specific entry from the 'db' table
*/

 // connect to the database
 include('connect-db.php');

 // check if the 'id' variable is set in URL, and check that it is valid

 // get id value
 $id = $_GET['id'];

 // delete the entry
 $result = mysql_query("DELETE FROM db WHERE 'Report No.'= '$id'")
 or die(mysql_error()); 

 // redirect back to the view page
 header("Location: view.php");

 // if id isn't set, or isn't valid, redirect back to view page
 {
 header("Location: view.php");
 }

?>

2 个答案:

答案 0 :(得分:0)

在表字段名称周围应用反引号(`)&#34;报告编号&#34; (它不是定义表字段名称的标准方法)

试试这个

$result = mysql_query("DELETE FROM db WHERE `Report No.`= '$id'");

答案 1 :(得分:-1)

通过删除名称表的单引号来修复您的查询:

$result = mysql_query("DELETE FROM db WHERE `Report No.`= '$id'");

确保为报告编号列名称键入权限。实际上,不建议命名为报告编号