PHP Mysql delete Query not working properly

前端 未结 3 1962
逝去的感伤
逝去的感伤 2021-01-22 05:24

I am pulling a list of products from my MYSQL database and using a delete button against each product in case the operator wants to delete the product.

The problem is th

3条回答
  •  天命终不由人
    2021-01-22 05:56

    Most likely because you setup the id="delete". Usually id attribute values are not duplicated.

    echo "
    "; echo "";

    The submit button gets the first ID and thus getting the first hidden input.

    Alternatively, you could devise your button like this and serve as your marker:

    No need to print each form!. Just wrap it with the table:

    echo "";
    
    echo '';
    while($row = mysqli_fetch_assoc($result)) {
        $prod_id = $row['prod_id'];
        echo "";
            echo "";
            echo "";
            echo "";
            echo "";
            echo "";
        echo '';
    }
    
    echo '
    ".$count."".$row['prod_id']."".$row['prod_name']."".$row['prod_price'].""; // each id is assigned to each button, so that when its submitted you get the designated id, the one that you clicked echo ""; echo "
    '; echo '';

    Then in PHP processing:

    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!';
        }
    }
    

提交回复
热议问题