-2

I am currently working on a delete function for my mysql database. I am not good at coding and find it very dificult. I need to learn how to make the delete function but I have been stuck for a while. You wil probably laugh at my code but as I said, its very hard for me. I hope someone can give me a tip or help me with my code.

When I run my code like this, nothing wil happen at delete.php

<html>
<head>
</head>
</body>
<table>

<tr> 
<th> Naam </th> <th> Soort </th> <th> Prijs </th> <th> Bijzonderheden </th> 
</tr>

<?php


$sth = $pdo->prepare('select * from gerechten');
$sth->execute();

while($row = $sth->fetch())
{
    
    
    echo "<tr> ";
        echo "<td>" . $row['Naam'] . "</td>"; 
        echo "<td>" . $row['Soort'] . "</td>";
        echo "<td> " . $row['Prijs'] . "</td>";
        echo "<td> " . $row['Bijzonderheden'] . "</td>";
echo "<td> <a href='Delete.php?gerechtID=".$row['ID']."'><button>Delete</button></a> <br> </td>";
    echo "</tr> "; 
    

}


// de update knoppen doorverwijzen met een href --> en dan een form maken voor 1 rij <-----------
?>

</table>

</html>

Delete.php

<?php

$id = $_GET['gerechtID'];
$servername = "localhost";
$username = "root";
$password = "";
$naam = $soort = $prijs = $bijzonderheden = "";
$foutmelding1 = $foutmelding2 = $foutmelding3 = $foutmelding4 = "";

try {
  $pdo = new PDO("mysql:host=$servername;dbname=restaurant", $username, $password);
  // set the PDO error mode to exception
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "Connected successfully";
} 
// connectie is mislukt
catch(PDOException $e) {
  echo "Connection failed: " . $e->getMessage();
}



$sth = $pdo->prepare('DELETE FROM gerechten WHERE ID = $id');
?>

1 Answers1

0

You are using prepared statements wrong by only preparing the DELETE statement but not binding any parameters and executing it.

https://www.php.net/manual/en/pdo.prepared-statements.php

$sth = $pdo->prepare('DELETE FROM gerechten WHERE ID = ?');
$sth->bindValue(1, $id);
$result = $sth->execute();
Code Spirit
  • 3,992
  • 4
  • 23
  • 34