mercredi 24 juin 2020

Output mysql database table with a checkbox column and query checked checkboxes in another file [duplicate]

The concept is to display mysql database table and add a column with a checkbox for each row. The user will just check the desired data and click a button which will direct the user to a new page.

I was successfully able to add a checkbox column but when i select a checkbox, nothing is displayed in the other page. I currently just have two files: index.php shows the table with checkbox and search.php shows the selected checkbox.

index.php

      <div style="overflow-x:auto;">
        <form action= 'search.php' method='post' name='check'>"
         <table id="test_data">
           <tr>
             <th> Select </th>
             <th> Test ID </th>
             <th> Path </th>
             <th> Video 1 Path </th>
             <th> Video 2 Path</th>
             <th> Video 3 Path</th>
             <th> Video 4 Path</th>
           </tr>

           <!--Prints out data from test_data table-->
    <?php
    $sql = "SELECT * FROM test_data";
    $result= mysqli_query($conn, $sql);
    $queryResults = mysqli_num_rows($result);

    if ($queryResults > 0) {
        // echo "<form action= 'ttt.php' method='post' name='check'>";
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<tr>
                    <td><input type='checkbox' name='checkbox_id[]' value='" . $row['test_id'] . "'></td>
                       <td> ".$row['test_id']." </td>
                       <td> ".$row['path']." </td>
                       <td> ".$row['video1_path']." </td>
                       <td> ".$row['video2_path']." </td>
                       <td> ".$row['video3_path']." </td>
                       <td> ".$row['video4_path']." </td>
                     </tr>";
        }
        echo "<input type='submit' name='update' value='Apply'/>";
        // echo "</form>";
        }
    ?>
         </table>
         <!-- <input type='submit' name='update' value='Apply'/> -->
        </form>
       </div>

search.php

<div style="overflow-x:auto;">
    <table id="test_data">
        <tr>
            <th> Test ID </th>
            <th> Path </th>
            <th> Video 1 Path </th>
            <th> Video 2 Path</th>
            <th> Video 3 Path</th>
            <th> Video 4 Path</th>
        </tr>

<script>
if (document.querySelector('form[name="check"] input[type="checkbox"]:checked')){

<?php
    if (!empty($_POST['checkbox_id'])) {
        $servername = "localhost";
        $username = "root";
        $password = "mysql";
        $database = "dat_simulation";
        $conn = new mysqli($servername, $username, $password, $database);
        // $checkbox_id = $_POST['checkbox_id'];
        if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    $num_var= " ";
    $num_var= mysqli_real_escape_string($conn,$_POST['checkbox_id']);
    $sql= "SELECT * FROM test_data WHERE test_id= '$num_var'";
    $result = mysqli_query($conn, $sql);
    if (!$result) {
        echo 'MySQL Error: ' . mysqli_error($conn);
        exit;
    }
    // $queryResults = mysqli_num_rows($result);

    if ($result->num_rows > 0) {
        while ($row = $result->mysqli_fetch_assoc()){
            $field1name = $row["test_id"];
            $field2name = $row["path"];
            $field3name = $row["video1_path"];
            $field4name = $row["video2_path"];
            $field5name = $row["video3_path"];
            $field6name = $row["video4_path"];

            echo "<tr>
                    <td> ".$field1name." </td>
                    <td> ".$field2name." </td>
                    <td> ".$field3name." </td>
                    <td> ".$field4name." </td>
                    <td> ".$field5name." </td>
                    <td> ".$field6name." </td>
                  </tr>";
    ?>
    <?php
        }
    } 
    $conn->close();
    }
    ?>

    }else {
        alert("Please select checkbox ");
    }
    </script>
          </table>
        </div>

When i inspect the code in the browser i get the following error:

Uncaught SyntaxError: Unexpected token '<' if (document.querySelector('form[name="check"] input[type="checkbox"]:checked')){
Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in..

Can someone help me in this?

Is there a simpler solution to query checked checkboxes from a database using php/js? Thank you




Aucun commentaire:

Enregistrer un commentaire