简体   繁体   中英

PHP : add a delete button in last column to delete a table row

I have a table displayed in HTML. I want to add a button delete in the last column to remove the considered line of the table. Thus, I create a form in my last column, with a hidden value, which is id (= primary key of my table entry), to pass id, through a POST method, in another page to launch an DELETE SQL query. The below code is not working :

<form action="delete_facture.php" method="post">
            <input type="hidden" name="id2" value="<?php $donnees['id'] ?>"/>
            <input type="submit" value="delete"/>
</form>

Than the delete_facture.php is the following :

<?php
// Connexion à la base de données
try
{
$bdd = new PDO('mysql:host=localhost;dbname=mydb', 'root', '');
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
$req = $bdd->prepare('DELETE FROM factures WHERE id= :id2');
$req->execute(array(
                    ':id2'=>$_POST['id2']
                    ));
header('Location: index.php');
?>

What's wrong in my code ? Thank you.

put echo , then only it will assign the value in the textbox. Otherwise you will get empty value. Try this,

<input type="hidden" name="id2" value="<?php echo $donnees['id'] ?>"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM