Mysql
 sql >> Base de données >  >> RDS >> Mysql

PHP Mysql delete Query ne fonctionne pas correctement

Probablement parce que vous avez configuré le id="delete" . Généralement, les valeurs d'attribut id ne sont pas dupliquées.

echo "<td><form action='delete_prod.php' id='delete' method='get'>";
echo "<button type='submit' form = 'delete' class='btn btn-default' name='delete'>Delete</button>";

Le bouton d'envoi obtient le premier ID et obtient ainsi la première entrée masquée.

Alternativement, vous pouvez concevoir votre bouton comme ceci et servir de marqueur :

Pas besoin d'imprimer chaque formulaire !. Enveloppez-le simplement avec le tableau :

echo "<form action='delete_prod.php' id='delete' method='get'>";

echo '<table>';
while($row = mysqli_fetch_assoc($result)) {
    $prod_id = $row['prod_id'];
    echo "<tr>";
        echo "<td>".$count."</td>";
        echo "<td>".$row['prod_id']."</td>";
        echo "<td>".$row['prod_name']."</td>";
        echo "<td>".$row['prod_price']."</td>";
        echo "<td>";
        // each id is assigned to each button, so that when its submitted you get the designated id, the one that you clicked
        echo "<button type='submit' value='$prod_id' class='btn btn-default' name='delete'>Delete</button>";
        echo "</td>";
    echo '</tr>';
}

echo '</table>';
echo '</form>';

Puis en traitement PHP :

if(isset($_GET['delete'])) // as usual
{
    include "connection.php";
    $prod_id = $_GET['delete']; // get the id
    // USE PREPARED STATEMENTS!!!
    $del="DELETE FROM products WHERE prod_id = ?";
    $delete = $link->prepare($del);
    $delete->bind_param('i', $prod_id);
    $delete->execute();
    // don't echo anything else, because you're going to use header
    if($delete->affected_rows > 0) {
        header('location:show_db.php');
    } else {
        echo 'Sorry delete did not push thru!';
    }
}