dimanche 11 décembre 2022

Deleting multiple rows with checkboxes - PHP and MySQL

I have an amateur website, where every user has an own ID and they can add and list elements with their own ID. Adding new items and listings working wonderful, but deleting multiple rows with checkboxes won't. I tried a lot of different versions, but I cannot find out what am doing wrong. Deleting is an external PHP file.

//del.php

(... connection, etc etc)

    $userFID = $_SESSION["id"];
        $edittable=$_POST["selector"];
    $N = count($edittable);
    for($i=0; $i < $N; $i++)
        {
            $myres = $db->prepare("DELETE * FROM products WHERE userFID='$userFID'");
            $myres->bindParam(":userFID", $edittable[$i], PDO::PARAM_STR);
            $myres->execute();
        }

I get this message: Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens (...) PDOStatement->execute() #1 {main} thrown in /home/.sites (...) on line 26

My other file:

(...)

<form action="del_item.php" method="post">
            <table class="table table-hover">
                <thead class="table-dark">
                    <tr>
                        <th> </th>
                        <th class="centertd">ProductID</th>
                        <th class="centertd">Productname</th>
                        <th class="centertd">Amount</th>
                        <th class="centertd">Added at</th>

                    </tr>
                </thead>
                <tbody>
                    <?php   
                        $userFID = $_SESSION["id"];
                        
                        $result = $db->prepare("SELECT * FROM products WHERE userFID='$userFID' ORDER BY productID ASC");
                        $result->execute();
                        for($i=0; $row = $result->fetch(); $i++){
                    ?>
                    
                    
                    
                    <tr>
                        <td class="centertd"><input name="selector[]" type="checkbox" value="<?php echo $row["productID"]; ?>"></td>
                    
                        <td class="centertd"><label><?php echo $row["productname"]; ?></label></td>
                        <td class="centertd"><label><?php echo $row["productamount"]; ?></label></td>
                        <td class="centertd"><label><?php echo $row["productadded"]; ?></label></td>
                        
                    </tr>
                    
                    
                    <?php } ?>
                    
                </tbody>
                
            </table>
                <input type="submit" value="delete" />
            </form>

I tried everything and I get always this message:

Invalid parameter number: number of bound variables does not match number of tokens in...

I have no more idea.




Aucun commentaire:

Enregistrer un commentaire