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

标签 php mysql database

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

脚本.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.

最佳答案

Firstof all your code is sql vulnerable

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

脚本.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>

关于php - 使用 PHP Mysqli 从下拉列表中选择多个选项时如何显示数据库中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44904627/

相关文章:

javascript - 如何使用 ajax 和 codeigniter 加载查看更多评论

php - 使用 xcode 8 (Objective-c) 将 Textfield 值发送到 PHP MySQL,有或没有点击操作?

mysql - MySQL Workbench 中的正向工程输出错误 1064

python - 有没有和 sqlite 一样的 nosql 平面文件数据库?

javascript - 如何在不使用按钮的情况下选择 html 表格中的一行?

php - 使用PHP将字符串分成相等的部分

php - 在 PHP 中比较两个 ISO8601 日期字符串

sql - 选择 X "closest"ids?

mysql - 删除sql中的重复项并相应地修改关系表

php - LAMP 环境变量 ($PATH) 出现问题