I'm selecting records from a table with a Delete button in the echo line. I want to be able to delete the record when I click on the corresponding Delete button. This is my delete query to delete the selected record from the other query, but it won't work!
$querytwo = 'DELETE FROM paginas WHERE id= $_POST["id"]';
I'm doing something wrong. What is it? Here is the full code.
<?php
include_once("config.php"); //this is the database connection
$query = "SELECT * FROM paginas "; //selects from the table called paginas
$result = mysqli_query($mysqli, $query);
while($row = mysqli_fetch_assoc($result))
{
$pagetitle = $row['pagetitle'];
$toevoeging = $row['toevoeging'];
$message = $row['message'];
echo '<article class="topcontent">
<div class="mct">
<h2>' . $pagetitle .'</h2>
</div><br>
<p class="post-info">'. $toevoeging . '</p>
<p class="post-text"><br>'.$message.'</p>
</article>
<div class="deleteknop">
<form method="post">';
echo '<input type="hidden" name="id" value="'.$row['id'].'">
<input name="delete" type="submit" value="Delete Now!">
</form>
</div>' ;
}
$querytwo = 'DELETE FROM paginas WHERE id= $_POST["id"]';
if (isset($_POST['delete'])) //Deletes the query if 'delete' button is clicked
{
$resulttwo = $mysqli->query($querytwo);
}
?>
Got it working. I changed the line to $querytwo = "DELETE FROM paginas WHERE id= $_POST[id]";
It's functional. Although there was a warning notice saying “Notice: Undefined index”
but I got rid of it thanks to error_reporting(0);