简体   繁体   中英

How to select a row(s) from dynamic table using checkbox (PHP) and insert those row to send mail function

Hi I have a page that displays all of the contents of my table. Alongisde each row of the table I also have a column containing a checkbox. When the user selects one or more rows by ticking the checkbox and pressing the submit button, I want just those rows to appear in a table on the next page (material.php). I know it is basically a select statement per row that is selected. But I dont know how to approach this. Can anybody help? Thanks

<td><input type='checkbox' name='material[]' value='material[<?php echo $id;?>]' /></td>

Here is my material.php page -

<table>
        <?php
        foreach ($_POST['material'] as $item) {
            $query = "SELECT * FROM model_material WHERE id = '$item'";

            if (mysqli_query($db_con, $query)) {
                $query_result = mysqli_query($db_con, $query);
                while ($all_select_material = mysqli_fetch_assoc($query_result)) {
                    extract($all_select_material);
                    ?>
                    <tr>
                        <td><?php echo $id; ?></td>
                        <td><?php echo $model_id; ?></td>
                        <td><?php echo $material_id; ?></td>
                    </tr>
                    <?php
                }
            }
        }
        ?>
    </table>

Please someone help

Try something along the lines of this: Using prepared statements for security:

<table>
        <?php
        foreach ($_POST['material'] as $item) {  // loop your post array
            $query = "SELECT col1,col2,col3,col4 FROM model_material WHERE id = 
             ?";   // prepare a query
            $stmt = $db_con->prepare($query);
            if ($stmt) {
               $stmt->bind_param("i",$item);  // bind the integer id ($item)
               $stmt->execute();    // fire it
               $stmt->bind_result($col1,$col2,$col3,$col4);  // bind the selected rows to each variable
               $stmt->store_result();   //incase of large results prevents memory error
                while ($stmt->fetch()) {   // loop your results like $result fetch assoc
                   echo "<td>".$col1."</td>";   // display data
                   echo "<td>".$col2."</td>";
                   echo "<td>".$col3."</td>";
                }
               $stmt->close();   // close statement
            }
          }
           $db_con->close();  // close database
        ?>
    </table>

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