我正在尝试从MySQL数据库中删除我的数据行。
下面是打印表格结果的查询,其中包含关键字。请参阅此图片,了解我的结果如何显示:
我成功地从我的addKeywordTable获取数据但是,我无法进行删除。这是我的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>";
?>
单击“删除”按钮后,您将进入确认页面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
?>
确认删除后,您将确认页面:“reallyDelete.php”。这是我遇到麻烦的页面。出于某种原因,我的变量不显示行/关键字名称的内容。
我的数据库中只有2列:KeyID和Keyword_Name
这是删除确认页面。我怎样才能将$ 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'>";
?>
对此的任何帮助都会很棒。下面是我的sql表的截图,用于addKeywordTable
答案 0 :(得分:0)
在deleteKey.php
中,您可能会收到错误:
require 'db.php';
$sel_key = $_POST[sel_key];
//SQL statement to select information
$sql = "SELECT * FROM addKeywordTable WHERE keyID = $sel_key";
第二行是否应该读取$sel_key = $_POST['sel_key'];
?
这将导致永远不会在后面的查询中选择记录。
答案 1 :(得分:0)
我看到你永远不会在你的文件 deleteKey.php 和 reallyDelete.php 中执行查询(也许你不想放完整代码)。
问候!