I am trying to delete my data row from my MySQL db.
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. Here is my index.php page which shows the input button that gets clicked to activate the deletion:
<?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:
<?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". 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
here is the delete confirmation page. How could I pass the $id to the db to delete the keyword/record.
<?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
In your deleteKey.php
you may have an error:
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'];
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).
Regards!