我試圖從數據庫中刪除一行,同時點擊刪除鏈接。但收到以下錯誤消息:試圖從數據庫中刪除一行使用PHP
您的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";
}
?>
請幫幫我。
[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護,[棄用過程](http://j.mp/Rj2iVR)已經開始。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 – hakre