I need to make a delete button that only the admin can see. The button needs to delete an item in my database but I'm having troubles with the last part.
I used this code to create the button and to call the delete function when it's clicked
if ($_SESSION['UserID'] == 1) {
echo '<button name="featureDelete"> Delete </button>' . '<br>';
if (isset($_POST['featureDelete'])) {
$deleteFeature = $feature->Delete($row);
}
}
And this is my delete function in my class
public function Delete($row)
{
$db = new db();
$sql= "DELETE FROM features WHERE FeatureID ='".$row['FeatureID']."'";
$db->conn->query($sql);
}
So I can see the button, but when I click it nothing happens, I even tried echo'ing something, but didn't get a result. What am I missing?
You have no form. Only a button.
echo '<form method="post"><button name="featureDelete"> Delete </button></form>' . '<br>';
You have to create a form for your button :
<form action="" method="post">
<input type="submit" name="featureDelete" value="Delete" />
</form>
you have to create a form and send the variable $row.
Try:
<form method="POST"> <input type="text" value="1" name="row" /> <button type="submit" >DELETE</button></form>
And then, on the PHP, before executing the SQL
$row = $_POST['row'];