Redzwan Latif
Redzwan Latif

Reputation: 886

One form, one button, one mysql row

This is my form code :

<?php
            $b = 0;
            $stmtb = $conn->prepare('SELECT * FROM picture WHERE id = :id');
            $stmtb->bindValue(':id', $id, PDO::PARAM_STR);
            $stmtb->execute();

            while($sb = $stmtb->fetch(PDO::FETCH_ASSOC)) {
            $pic = $sb["pic"];
            $picid = $sb["picid"];
            ?>


            <img src="/pibgcarta/<?php echo $pic; ?>" height="10%" width="45%" />


            <form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']."?dm=".$sch; ?>" id="picform">

            <input type="hidden" name="picid" id="picid" value="<?php echo $picid; ?>" class="input_field" role="input" aria-required="true" />

            <input type="hidden" name="pic" id="pic" value="<?php echo $pic; ?>" class="input_field" role="input" aria-required="true" />


            <input type="submit" value="Padam" class="submit_btn" name="deletepic" id="deletecarta" />


            <?php ++$b; } ?>

This is my post code :

if(isset($_POST['deletepic'])) {


    $picid = $_POST["picid"];
    $pic = $_POST["pic"];



    include "connection.php";
    $query = "DELETE FROM picture WHERE picid = :id";
            $statement = $conn -> prepare($query);
            $statement -> BindParam('id', $picid, PDO::PARAM_INT);
            $result = $statement -> execute();


    $dpic = "/pibgcarta/".$pic;

    unlink($dpic); 




}

Now my problem is, let's say there are 3 pictures. I pressed the delete button for the second picture but the third picture that was deleted. When I press the delete button for first picture, the second picture is deleted. Can someone help me with my problem? Really appreciate your help. Thanks

Upvotes: 0

Views: 69

Answers (1)

ickmund
ickmund

Reputation: 289

You don't seem to be closing your forms. So in the html, you'll have several hidden inputs with name pickid. Only the last one will be available for you in your script.

So after your submit button, add </form>

Upvotes: 3

Related Questions