0

我正在尝试从 mysql 数据库中删除数据。但是当我查看代码时,我收到以下错误无法删除数据:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“WHERE ProductsID = 30”附近使用正确的语法

<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>
4

1 回答 1

1

您的查询是错误的,您缺少 'from' 将其更改为此

"DELETE from Products WHERE ProductsID = '$ProductsID'" ;
于 2013-04-27T07:59:07.183 回答