如何使用PHP Mysqli从Dropdown中选择多个选项时显示数据库中的数据

时间:2017-07-04 11:20:59

标签: php mysql database

脚本页面运行良好。当我在下一个仪表板页面中选择多个选项时,不会显示任何记录。请解决这个问题。我认为所选值无法在仪表板页面中识别

的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

<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. Doesnot show record for selected items.

2 个答案:

答案 0 :(得分:0)

我会通过以下方式接近它:

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

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

我将它拆分为几个变量,以便您了解该过程。 希望我能帮忙!

我希望你的理解根本不安全,我建议你多读一些关于准备好的陈述: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

答案 1 :(得分:0)

  

首先,您的所有代码都是易受攻击的

在Scrip中,您没有在<select>标记中定义选项的值。首先定义值,为此需要从数据库中获取

<强>的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>

在仪表板中执行适当的标记

<强> 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>