2013-04-27 21 views
0

我想從mysql數據庫中刪除數據。但是,當我查看代碼時,我收到以下錯誤無法刪除數據:您的SQL語法中有錯誤;檢查對應於你的MySQL服務器版本正確的語法使用手動附近「WHERE ProductsID = 30」在行1錯誤:無法刪除數據:您在'WHERE ProductsID = 30'附近的SQL語法中有錯誤任何想法?

<html> 
<head> 
<title>Delete a Record from MySQL Database</title> 
</head> 
<body> 

<?--Code to delete--> 
<?php 
if(isset($_POST['delete'])) 
{ 
$dbhost = 'DatabaseHostName'; 
$dbuser = 'username'; 
$dbpass = 'password'; 
$conn = mysql_connect($dbhost, $dbuser, $dbpass); 
if(! $conn) 

{ 
    die('Could not connect: ' . mysql_error()); 
} 

$ProductsID = $_POST['ProductsID']; 

$sql = "DELETE Products ". 
     "WHERE ProductsID = $ProductsID" ; 

mysql_select_db('dbname'); 
$retval = mysql_query($sql, $conn); 

if(! $retval) 
{ 
    die('Could not delete data: ' . mysql_error()); 
} 
echo "Deleted data successfully\n"; 
mysql_close($conn); 
} 
else 
{ 
?> 

<form method="post" action="<?php $_PHP_SELF ?>"> 
<table width="400" border="0" cellspacing="1" cellpadding="2"> 
<tr> 
    <td width="100">Products ID</td> 
    <td><input name="ProductsID" type="text" id="ProductsID"></td> 
</tr> 

<tr> 
    <td width="100"> </td> 
    <td> </td> 
</tr> 

<tr> 
    <td width="100"> </td> 
<td> 
    <input name="delete" type="submit" id="delete" value="Delete"> 
</td> 
</tr> 
</table> 
</form> 

<?php 
} 
?> 
</body> 
</html> 
+0

[DELETE語法](http://dev.mysql.com/doc/refman/5.5/en/delete.html)。錯誤信息非常清楚。你不需要發佈100行不相關的代碼。 – 2013-04-27 07:59:48

回答

1

您所查詢的是錯的,你缺少「從」改成這樣

"DELETE from Products WHERE ProductsID = '$ProductsID'" ; 
+0

奇妙 - 它完美的作品。謝謝 – user2326279 2013-04-27 08:01:18

+0

這裏不需要引號,因爲它不是字符串ID :) – 2013-04-27 08:01:54

+0

這很好,但它是一個更安全的方式執行sql是不是:) – 2013-04-27 08:02:44