[英]Delete Row from DB using PHP/MYSQL
I am trying to delete my data row from my MySQL db. 我正在尝试从MySQL数据库中删除数据行。
Below is the query which prints the result of a table with keywords in them. 下面是查询,该查询显示带有关键字的表的结果。 See this pic to see how my results show up:
请看这张图片,看看我的结果如何显示:
I am successfully able to get my data from my addKeywordTable however, I can not get the deletion to take place. 我可以从addKeywordTable中成功获取数据,但是无法进行删除。 Here is my index.php page which shows the input button that gets clicked to activate the deletion:
这是我的index.php页面,其中显示输入按钮,单击该按钮可激活删除操作:
<?php
include 'db.php';
$sql = mysqli_query($con,"SELECT * FROM addKeywordTable ORDER BY
Keyword_Name ASC");
print <<<HERE
<table id="home">
HERE;
while ($row = mysqli_fetch_array($sql))
{
$key = $row['Keyword_Name'];
print <<<HERE
<tr><td>
<form method="post" formenctype="multipart/form-data" formmethod="POST"
value="Delete" action="deleteKey.php">
<tr>
<input type="hidden" name="sel_key" value="$id">
<input type="submit" name="delete" value=" Delete " id="deleteKey" > $key
<hr/></tr>
</form></td></tr>
HERE;
}
print "</tr></table></body></html>";
?>
When you click the "Delete" button, you are taken to the confirm page deleteKey.php which looks like this: 单击“删除”按钮时,将转到确认页面deleteKey.php,如下所示:
<?php
require 'db.php';
$sel_key = $_POST[sel_key];
//SQL statement to select information
$sql = "SELECT * FROM addKeywordTable WHERE keyID = $sel_key";
//loop through record and get values
while ($key = mysqli_fetch_array($result)) {
$id = $key['Keyword_Name'];
} // end while loop
$pageTitle = "Delete a Keyword";
print <<<HERE
<div id="form1profile">
<h2>Are you sure you want to delete this record?<br/>
It will be permanently removed:</h2>
<ul>
<li>Keyword Category:<br/></li>
$key;
</ul>
<p><br />
<form method="post" action="reallyDelete.php">
<input type="hidden" name="id" value="$id">
<input type="submit" name="reallydelete" value="Confirm Delete" />
<input type="button" name="cancel" value="cancel"
onClick="location.href='addProfile.php'" /></a>
</p></form></div>
HERE;
// close else
?>
and after you confirm the deletion, you are then taken to confirm page: "reallyDelete.php". 在确认删除后,您将进入确认页面:“ reallyDelete.php”。 This is the page I am having trouble with.
这是我遇到问题的页面。 For some reason, my variable does not display the contents of the row/keyword name.
由于某种原因,我的变量不显示行/关键字名称的内容。
My DB only has 2 columns in it: KeyID and Keyword_Name 我的数据库中只有2列:KeyID和Keyword_Name
here is the delete confirmation page. 这是删除确认页面。 How could I pass the $id to the db to delete the keyword/record.
如何将$ id传递给数据库以删除关键字/记录。
<?php
include 'db.php';
$id = $_POST[id];
$sql = "SELECT * FROM addKeywordTable WHERE Keyword_Name = '$id'";
while ($row = mysqli_fetch_array($result)) {
$id = $row['Keyword_Name'];
} // end while loop
print "<p> $row has been permanently deleted.</p>";
$sql="DELETE FROM addKeywordTable WHERE Keyword_Name = '$id'";
echo "<meta http-equiv='Refresh' content='2; URL=addKeyword.php'>";
?>
Any help with this would be great.Below is a screenshot of my sql table for addKeywordTable 任何帮助都会很棒。下面是我的addKeywordTable的sql表的屏幕截图
In your deleteKey.php
you may have an error: 在您的
deleteKey.php
您可能会遇到错误:
require 'db.php';
$sel_key = $_POST[sel_key];
//SQL statement to select information
$sql = "SELECT * FROM addKeywordTable WHERE keyID = $sel_key";
Should that second line read $sel_key = $_POST['sel_key'];
第二行应该读
$sel_key = $_POST['sel_key'];
instead? 代替?
This would cause the record to never be selected in the query that follows. 这将导致在随后的查询中永远不会选择该记录。
I saw is that you never execute the queries in your file deleteKey.php and reallyDelete.php (maybe you didn't want put the full code). 我看到的是,您永远不会在文件deleteKey.php和trueDelete.php中执行查询(也许您不想放入完整的代码)。
Regards! 问候!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.