-1

我正在尝试在单击删除链接时从数据库中删除一行。但收到以下错误消息:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '' 附近使用正确的语法

下面是代码:

remove_db.php

<html>
<body>

<?php
//session_start();
$myusername = $_SESSION['uname'];


$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="vacation_tool"; // Database name
$tbl_name="login"; // Table name

// Connect to server and select databse.
$con = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$query="select EmployeeID,FirstName,LastName,ManagerName from users";

$result=mysql_query($query);

$num=mysql_numrows($result) or die ("error in function_name: <br>Sql: " . $sql . "<br>" . mysql_error());

//mysql_close();
?>
<div id="bv_Text2" style="position:absolute;left:480px;top:250px;width:550px;height:16px;z-index:5;" align="left">
<table border="1" cellspacing="2" cellpadding="2">
<tr width = "100%">
<td width = "15%" align = "center"><font style="font-size:13px" color="#000000" face="Arial"><b>Employee ID</b></font></td>
<td width = "20%" align = "center"><font style="font-size:13px" color="#000000" face="Arial"><b>First Name</b></font></td>
<td width = "20%" align = "center"><font style="font-size:13px" color="#000000" face="Arial"><b>Last Name</b></font></td>
<td width = "20%" align = "center"><font style="font-size:13px" color="#000000" face="Arial"><b>Manager Name</b></font></td>

</tr>
</div>


<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"EmployeeID");
$f2=mysql_result($result,$i,"FirstName");
$f3=mysql_result($result,$i,"LastName");
$f4=mysql_result($result,$i,"ManagerName");

?>
<div id="bv_Text2" style="position:absolute;left:480px;top:260px;width:144px;height:16px;z-index:6;" align="left">
<tr>
<td align = "center"><font style="font-size:13px" color="#000000" face="Arial"><?php echo $f1; ?></font></td>
<td align = "center"><font style="font-size:13px" color="#000000" face="Arial"><?php echo $f2; ?></font></td>
<td align = "center"><font style="font-size:13px" color="#000000" face="Arial"><?php echo $f3; ?></font></td>
<td align = "center"><font style="font-size:13px" color="#000000" face="Arial"><?php echo $f4; ?></font></td>
<td align = "center"><font style="font-size:13px" color="#000000" face="Arial"><?php echo '<td><a href="remove.php?EmployeeID=' . $num['EmployeeID'] . '">Remove</a></td>'; ?></font></td>
</tr>
</div>
<?php
$i++;
}
?>
</body>
</html>

和 remove.php :

<?php

session_start();
$myusername = $_SESSION['uname'];

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="vacation_tool"; // Database name
$tbl_name="login"; // Table name

// Connect to server and select databse.
$con = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form

if (isset($_GET['EmployeeID']))
 {
 // get id value
 $EmployeeID = $_GET['EmployeeID'];

 // delete the entry

 $query = "DELETE FROM users WHERE EmployeeID = $EmployeeID";

 $result = mysql_query($query) or die(mysql_error());

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

 ?>

请帮我。

4

3 回答 3

2

代替

$query = "DELETE FROM users WHERE EmployeeID = $EmployeeID";

$query = sprintf("DELETE FROM users WHERE EmployeeID ='%s'",
mysql_real_escape_string($EmployeeID));
于 2012-10-28T20:37:38.990 回答
0

您的代码状态

<a href="remove.php?EmployeeID=' . $num['EmployeeID'] . '">Remove</a></td>'; ?></font>

但是EmployeeID在里面$f1,不在里面$num$num甚至不是一个数组。

所以除了上面的错误(你应该使用过$f1)之外,你正在做一些其他的事情,你可以做的不是那么好:

  • 您还没有配置 PHP 来通知您可能收到的所有警告、错误等。
  • 您正在接受来自外部的值并在查询中使用它,从而使自己暴露于 SQL 注入攻击(EmployeeID = (int)$_GET['EmployeeID']至少使用)
  • 您正在使用不鼓励mysql_*使用的函数。请改用 PDO 或 mysqli。
  • 您没有将 PHP 代码与 HTML 演示文稿分开。

虽然完全有可能在不注意上述任何提示的情况下进行编程,但您会发现遵循它们更容易、更有效率并且更有趣。我为这种傲慢的语气道歉,但我很难发现这一点。

于 2012-10-28T20:38:41.253 回答
0

看起来您传递了错误的员工 ID。员工 ID 分配给 $f1,并且您在 url 中使用 $num['employeeID'],它应该为空。

删除链接必须是:

  <td align = "center"><font style="font-size:13px" color="#000000" face="Arial"><?php echo '<td><a href="remove.php?EmployeeID=' . $f1 . '">Remove</a></td>'; ?></font></td>

逐步调试总是有帮助的,这样您就可以自己弄清楚。在这种情况下 :

  1. HTML查看源码,并查看URL
  2. 打印 $_GET['employeeID']
  3. 打印删除sql

注意:不推荐使用 mysql_query 函数。使用 PDO 或 mysqli 函数。此外,在直接用于任何 DML 语句之前,必须对 $_GET 进行清理。完全脆弱。

于 2012-10-28T20:39:33.373 回答