简体   繁体   English

使用简单的表格和PHP删除mySQL记录

[英]Use a simple form and PHP to delete a mySQL record

I am trying to build a page that will allow the user to enter an employee number via a form and when they hit the "delete" button it will remove the corresponding record. 我正在尝试构建一个页面,该页面将允许用户通过表格输入员工编号,并且当他们单击“删除”按钮时,它将删除相应的记录。 The database is named "Crosshill", the Table is called "Employees" and the field I want to use is "employeeid". 该数据库名为“ Crosshill”,表名为“ Employees”,我要使用的字段为“ employeeid”。

It seems to connect fine to the DB, but the code below doesn't work. 似乎可以很好地连接到数据库,但是下面的代码不起作用。 When you hit the "Delete" button it returns an error of: 当您点击“删除”按钮时,它将返回以下错误:

Could not delete data: You have an error in your SQL syntax; 无法删除数据:您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE employeeid =' at line 1 Blockquote 检查与您的MySQL服务器版本相对应的手册,以在第1行Blockquote的'WHERE employeeid ='附近使用正确的语法


<html>
<head>
<title>Delete an Employee</title>
</head>
<body>

<h3>Enter the Employee Number below to delete a record</h3>

<?php
if(isset($_POST['delete']))
{
$dbhost = '####';
$dbuser = '####';
$dbpass = '####';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

$employeeid = $_POST['employeeid'];

$sql = "DELETE Employees ".
       "WHERE employeeid = $employeeid" ;

mysql_select_db('Crosshill');
$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">Employee ID</td>
<td><input name="employeeid" type="number" id="employeeid"></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
}
?>
</html>

它是DELETE FROM <table> WHERE <condition> ,您的查询中缺少FROM

You are missing "from" after delete.. It should be as DELETE from Employees WHERE condition. 删除后,您缺少“来自”。应为“从雇员那里”条件下的“删除”。

To avoid such situations always do one thing, just echo the sql query and using "exit" after the same to terminate the further execution of the program. 为避免出现这种情况,请总是做一件事,只需回显sql查询并在该查询后使用“退出”即可终止程序的进一步执行。

Copy the query from browser and run the same in phpmyadmin or whatever other tool you use.. 从浏览器复制查询,并在phpmyadmin或您使用的任何其他工具中运行该查询。

That practice will help you to find out the root cause of the problem.. 这种做法将帮助您找出问题的根本原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM