简体   繁体   English

使用PHP Mysqli从下拉列表中选择多个选项时,如何显示数据库中的数据

[英]How to display data from database When Selecting Multiple option from Dropdown using PHP Mysqli

Script Page is working nicely. 脚本页面运行良好。 When I select the multiple options in next dashboard page, no records display. 当我在下一个仪表板页面中选择多个选项时,不会显示任何记录。 Please fix this problem. 请解决此问题。 I think the selected value cannot recognize in dashboard page 我认为所选值无法在仪表板页面中识别

Script.php script.php的

<?php include("connection.php") ?>
<form id="script" name="script" action="dashboard.php" method="post">
    <strong>Choose Script Name : </strong><select name="script[]" id="select3" multiple=multiple style="margin: 20px;width:300px;">   
        <?php
        $result = $conn->query("select script_name from script_details ORDER BY script_name");
        while ($row = $result->fetch_assoc()) {
            unset($script_name);
            $script_name = $row['script_name'];
            echo '<option value="' . $id . '">' . $script_name . '</option>'; // Generated From database
        }
        ?>
    </select>
    <input type="submit" name="submit" id="button" value="View Dashboard" />
</form>

Dashboard.php Dashboard.php

<table border="1">
    <tr align="center">
        <th>Number </th>      <th>Script Name</th>    <th> Date</th> 
    </tr> 
    <?php
    include("connection.php");
    $select = $_POST['script'];
    $selects = "SELECT * FROM script_details where script_name='$select'";
    $result = $conn->query($selects);
    echo "<table>";
    while ($row = $result->fetch_assoc()) {
        echo "<tr><td>" . $row["id"] . "</td><td>" . $row["script_name"] . "</td></tr>" . "</td><td>" . $row["date"] . "</td></tr>";
    }
    echo "</table>";
[This is script page Image. Selecting option from script_details database. Field name : script_name.][1]?>

This is Dashboard page. 这是“仪表板”页面。 when selecting script2, script3 option. 选择script2,script3选项时。 Doesnot show record for selected items. 不显示所选项目的记录。

I would approach it in the following way: 我将通过以下方式进行处理:

$scriptsArr = $_POST['script'];
$scriptsStr = implode(',', $scriptsArr);

$selects = "SELECT * FROM script_details where script_name IN ($scriptsStr)";

I've split it to few variables so you can understand the process. 我将其拆分为几个变量,以便您可以了解过程。 Hope I could help! 希望我能帮上忙!

I hope your understand is not safe at all, I would suggest you will read a bit more about prepared statements: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php 希望您的理解一点都不安全,建议您阅读更多有关准备好的语句的信息: http : //php.net/manual/en/mysqli.quickstart.prepared-statements.php

Firstof all your code is sql vulnerable 首先,您的代码易受sql攻击

In Scrip you didn't define values of options in <select> tag. 在Scrip中,您没有在<select>标记中定义选项的值。 define value first and for this you need to fetch is from database 首先定义值,为此您需要从数据库中获取

Script.php script.php的

<?php include("connection.php") ?>
<form id="script" name="script" action="dashboard.php" method="post">
    <strong>Choose Script Name : </strong>
    <select name="script[]" id="select3" multiple=multiple style="margin: 20px;width:300px;">   
        <?php
        $result = $conn->query("select id, script_name from script_details ORDER BY script_name");
        while ($row = $result->fetch_assoc()) {
            unset($script_name);
            $script_name = $row['script_name'];
            $id = $row['id'];
            echo '<option value="' . $id . '">' . $script_name . '</option>'; // Generated From database
        }
        ?>
    </select>
    <input type="submit" name="submit" id="button" value="View Dashboard" />
</form>

In dashboard do proper markup 在仪表板中进行适当的标记

Dashboard.php Dashboard.php

<table border="1">
    <tr align="center">
        <th>Number </th>      <th>Script Name</th>    <th> Date</th> 
    </tr> 
    <?php
    include("connection.php");
    $select = $_POST['script'];
    $ids = "'" . implode("','", $select) . "'";
    $selects = "SELECT * FROM script_details WHERE id IN ($ids)";
    $result = $conn->query($selects);
    while ($row = $result->fetch_assoc()) {
        echo "<tr>"
                . "<td>" . $row["id"] . "</td>"
                . "<td>" . $row["script_name"] . "</td>"
                . "<td>" . $row["date"] . "</td>"
            . "</tr>";
    }
    ?>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM