繁体   English   中英

使用PHP和JavaScript下拉菜单查询MySQL

[英]Querying MySQL with PHP and javascript drop down menu

我已经看了几天了,但是还没有找到解决我问题的方法。 我正在编写一些PHP,以从在WAMP服务器上设置的MySQL数据库进行查询。 我也在学习PHP和HTML javascript,所以两种语言的语法对我来说还是有点陌生​​。

我的目标是用Java编写一个下拉选择器框,该框允许用户选择要应用于选择查询的过滤器,如下所示:

SELECT * from exampletable WHERE header = "selected_option" 

其中“ exampletable”是SQL数据库中现有的表,“ header”是该表中的一列,“ selected option”是用户从下拉菜单中选择的内容。

我已经尝试使用$_POST超全局变量编写各种HTML形式的动作,这些动作将调用包含SQL查询的PHP文件,但是似乎没有任何效果。 任何建议和解决方案示例都将是惊人的。

谢谢!

index.php(index.php是用户界面的前端)

<!DOCTYPE HTML>
<html>
<form action="search.php" method="post">
    <select name="family">
            <option value="" selected="selected">Any family</option>
                <option value="capacitory">capacitor</option>
                <option value="resistor">resistor</option>
                <option value="ferrite bead">ferrite bead</option>
    </select>
    <input name="search" type="submit" value="Search>
</form>
</html>

search.php(search.php接收选定的选项值并将其传递到MySQL查询中)

<!DOCTYPE HTML>
<html>
<head>
<style>
table {
    width: 100%;
    border-collapse: collapse;
}

table, td, th {
    border: 1px solid black;
    padding: 5px;
}

th {text-align: left;}
</style>
</head>

<body>
<?php

$con = mysqli_connect('localhost','root','kelly188','mysql');
mysqli_select_db($con,"testv2");

$varfam = $_POST['family'];

$query = "SELECT * FROM testv2 WHERE (family = $varfam)";

$result = mysqli_query($query);

if($result)
{
while ($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['family']."</td>";
}
} else {
die(mysqli_error());
}
?>
</body>
</html> 

在此处输入图片说明

您应该使用准备好的语句来防止SQL注入。 mysql_fetch_array函数已从最新版本的PHP中删除。 像下面这样的东西会更理想。

if ($stmt = $con->prepare("SELECT * FROM testv2 WHERE (family = ?)")) {

    $stmt->bind_param("s",  $_POST['family']);
    $stmt->execute();

    $result = $stmt->get_result();

    while ($row = $result->fetch_assoc()) {

        echo "<tr>";
        echo "<td>".htmlentities($row['family'])."</td>";
        echo "</tr>";

    }   

    $stmt->close();
}

参见PHP文档: http : //php.net/manual/en/mysqli.prepare.php

的index.php

<form action="search.php" method="post">
<select name="family">
            <option value="" selected="selected">Any family</option>
            <option value="capacitory">capacitor</option>
            <option value="resistor">resistor</option>
            <option value="ferrite bead">ferrite bead</option>
</select>
<input name="search" type="submit" value="Search"/>
</form>

search.php中

<?php
//////////////////////////////////
// Connect to database using PDO
$servername = "localhost"; 
$username = "test";
$password = "";
$dbname = "test_db";
$db_conn = new PDO("mysql:host=$servername;dbname=$dbname", $username,$password);
// End of database connection
////////////////////////////////////

if(isset($_POST['search']))
{
$family = $_POST['family'];
if(empty($_POST['family']))
{
$stmt = $db_conn->prepare("SELECT * FROM testv2");
$stmt->execute();
//we get the data
while($data = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo $data['family'];
echo "<hr>";    
}   
}
else
{   
$stmt = $db_conn->prepare("SELECT * FROM testv2 WHERE family = :family");
$stmt ->bindParam(':family', $family);
$stmt->execute();
//we get the data
while($data = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo $data['family'];
echo "<hr>";    
}
}
}
?>

暂无
暂无

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

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