[英]PHP buttons to delete a row in MYSQL table output
My goal is to display the entire table eg SELECT * FROM athlete and include button for each row in the table output, that when pressed will remove that row from the database. 我的目标是显示整个表格,例如SELECT * FROM sports并在表格输出中的每一行都包含一个按钮,当按下该按钮时,将从数据库中删除该行。
I have followed another post and shown below is what I have so far but does not work: 我关注了另一篇文章,下面是到目前为止的内容,但是没有用:
//if(isset($_POST['delete_id']) && (!empty($_POST['delete_id'])))
if(isset($_POST['Delete'])){
$delete_id = mysql_real_escape_string($_POST['delete_id']);
echo("deletebutton" . $delete_id);
//mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id);
//header('Location: main.php');
}
else
{
$query = 'SELECT * FROM athlete';
$result = mysql_query($query);
$self = $_SERVER['PHP_SELF'];
$self = htmlentities($_SERVER['PHP_SELF']); // strip tags to improve security
echo("<table>
<tr>
<td> id </td>
<td>last name</td>
<td>first name</td>
<td>delete data</td>
</tr>");
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
echo("<tr>");
echo("<td>" . $row['id'] . "</td>");
echo("<td>" . $row['lastName'] . "</td>");
echo("<td>" . $row['firstName'] . "</td>");
echo("<td> <form action='$self' method='post'>
<input type='hidden' name='delete_id value='$id' />
<input type='submit' value='Delete' />
</form>
</td>
</tr>");
}
mysql_free_result($result);
echo("</table>");
}
However this does not work. 但是,这不起作用。 Does anyone know why.
有谁知道为什么。 NOTE: I have left my login credentials out but I am connecting to the database elsewhere in the file.
注意:我已经保留了登录凭据,但是我正在连接到文件中其他位置的数据库。
The most probable cause is that your checking for $_POST['Delete'] having not given any of the submit buttons the attribute 'name' of 'Delete'. 最可能的原因是您对$ _POST ['Delete']的检查没有给任何提交按钮赋予'Delete'属性'name'。 However, the line you have commented out above should work?
但是,您上面注释掉的那行应该行吗?
I think you should look into this areas 我认为您应该研究这方面
//if(isset($_POST['delete_id']) && (!empty($_POST['delete_id'])))
if(isset($_POST['Delete'])){ if(isset($ _ POST ['Delete'])){
and 和
<input type='submit' value='Delete' />
You should include: <input type='submit' name="Delete" value='Delete' />
您应该包括:
<input type='submit' name="Delete" value='Delete' />
That should solve the problem. 那应该解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.