如何使用HTML中的选择框在数据库中搜索特定用户并在表中显示该信息?

时间:2019-06-02 16:34:55

标签: php html sql

我有一个选择框,用于显示数据库中所有用户的名称,但是,我需要在组合框上使用选定用户的“查找按钮”,以显示附加到该用户的数据。桌子

当前显示所有用户数据的表

<?php include("connection.php");
mysqli_query($link, "DELETE FROM `msg` WHERE `name` = '$adminmsgn' AND `email`= '$adminmsge' AND `msg`= '$adminmsgm'");
header("Location: http://localhost:8080/contact/admincp.php");
?>

这是在选择框中显示用户的表格

<table class="table table-hover">
    <thead class="thead-dark"></thead>
    <tr>
        <th scope="col">Shift ID</th>
        <th scope="col">Name</th>
        <th scope="col">Origin</th>
        <th scope="col">Destination</th>
        <th scope="col">Date</th>
    </tr>
    </thead>
    <?php
    global $result, $query;
    $sql = "SELECT * FROM shifts";
    $result = $db->query($sql);
    //Fetch Data form database
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "<tr><td>" . $row["shift_id"]. "</td><td>" . $row["name"] . "</td><td>"  . $row["origin"] . "</td><td>"  . $row["destination"] . "</td><td>"  . $row["date"] . "</td><td>"
                . $row["password"]. "</td></tr>";
        }
        echo "</table>";
    } else { echo "0 results"; }
       ?>
</table>

我正在尝试做到这一点,以便当按下组合框中的选定用户并按下“查找”按钮时,找到的数据将全部放入上述表中。

我可能会尝试将变量附加到选择框,并将其与数据库中的名称字段进行比较。

类似这样的东西

<form name="form1" method="POST" action="">
    <select name="getUser">
        <?php
        $res = mysqli_query($db, "SELECT * FROM shifts");
        while ($row = mysqli_fetch_array($res))
        {
            ?>
            <option><?php echo $row ["name"]; ?></option>
            <?php
        }
        ?>
    </select>
    <button class="btn-primary rounded">Find</button>
</form>

谢谢。

2 个答案:

答案 0 :(得分:0)

首先将用户ID回显到选项的值

$_POST

然后,当您提交表单时,您将从$userId = $_POST['getUser'];

获取值
$servername = "localhost";
$username = "username";
$password = "password";



try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
//something like this
$query = $conn->prepare("SELECT * FROM shifts WHERE id = :id");
$query->bindParam(':id',$userId,PDO::PARAM_INT);
$query->execute()
return $query->fetchAll();// I realised you wanted to get all the shifts so you don want fetchAll(),

不是您可以使用该变量查询数据库,但是您永远不要直接输入它,而应该使用PDO准备好的语句来防止注入。

std::mutex

请注意,在mysql中,与php不同,我们仅使用单个=进行比较。另外,我已经将名称更改为数据库中的唯一行,因为除非您的名称字段是唯一的,否则您如何知道要使用哪种名为Dan的用法?

如果要执行此操作而不重新加载整个页面,则需要使用Ajax并通过jQuery传递option标记的值。 这里是一些开始的地方:

https://www.w3schools.com/php/php_mysql_connect.asp

https://www.w3schools.com/xml/ajax_intro.asp

答案 1 :(得分:-1)

如果您对javascript(AJAX)不满意,请尝试使用表单

<?php $res = mysqli_query($db, "SELECT * FROM shifts"); ?>
<form name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"
    <select name="getUser">
        <option value='All'>All</options>
        <?php
            while ($row = mysqli_fetch_array($res)) { ?>
                <option value='$row ["name"]'><?php echo $row ["name"]; ?></option>
        <?php } ?>
    </select>
    <button class="btn-primary rounded">Find</button>
</form>

在你的桌子上

<table class="table table-hover">
    <thead class="thead-dark"></thead>
    <tr>
        <th scope="col">Shift ID</th>
        <th scope="col">Name</th>
        <th scope="col">Origin</th>
        <th scope="col">Destination</th>
        <th scope="col">Date</th>
    </tr>
    </thead>
    <?php
    global $result, $query;
    if ($_POST['getUser'] == 'All'){
        $sql = "SELECT * FROM shifts";
    } else {
        $sql = "SELECT * FROM shifts WHERE name = " . $_POST['getUser'];
    }
    $result = $db->query($sql);
    //Fetch Data form database
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "<tr><td>" . $row["shift_id"]. "</td><td>" . $row["name"] . "</td><td>"  . $row["origin"] . "</td><td>"  . $row["destination"] . "</td><td>"  . $row["date"] . "</td><td>"
                . $row["password"]. "</td></tr>";
        }
        echo "</table>";
    } else { echo "0 results"; }
       ?>
</table>