我正在尝试在点击删除链接时从数据库中删除一行。但收到以下错误消息:
您的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";
}
?>
请帮帮我。
答案 0 :(得分:2)
替换
$query = "DELETE FROM users WHERE EmployeeID = $EmployeeID";
与
$query = sprintf("DELETE FROM users WHERE EmployeeID ='%s'",
mysql_real_escape_string($EmployeeID));
答案 1 :(得分:0)
您的代码说明
<a href="remove.php?EmployeeID=' . $num['EmployeeID'] . '">Remove</a></td>'; ?></font>
但EmployeeID
位于$f1
,而不是$num
。 $num
甚至不是数组。
所以除了上面的错误(你应该使用$f1
)之外,你还做了其他一些不太好的事情:
EmployeeID = (int)$_GET['EmployeeID']
)mysql_*
个函数,其用法为discouraged。请改用PDO或mysqli。虽然完全有可能在不注意上述任何提示的情况下进行编程,但您会发现它更容易,更高效,并且更加简单更有趣。我为光顾的语气道歉,但我发现这很难。
答案 2 :(得分: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>
一步一步调试总是有帮助的,这样你就可以自己搞清楚了。在这种情况下:
注意:不推荐使用mysql_query函数。使用PDO或mysqli函数。 此外,在直接使用任何DML语句之前,必须对$ _GET进行清理。完全是脆弱的。