使用 PHP 和 mysqli 从数据库中获取 Selectbox 值
Getting Selectbox values from the database using PHP and mysqli
我想从 phpmyadmin 数据库中提取人名并将它们放在 HTML select 框中,当用户从 select 框中选择一个名字时它应该显示table 中该人的数据库详细信息。我似乎无法让它工作,我可以从数据库中获取名称并显示在 select 框中,但是当您单击名称时,它似乎会显示数据库中的每条记录而不仅仅是那个人的那个。我使用 mysql 而不是 mysql。这是我的代码
这是我的后端内容
<?php
$conn = mysqli_connect("localhost", "root", "root") or die ("No connection");
mysqli_select_db($conn, "flat") or die("db will not open");
$query = "select FlatCode, Address from FLAT";
$result = mysqli_query($conn, $query) or die("Invalid query");
echo "<table border='1'><tr><th>modulecode</th><th>studentnum</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td></tr>";
}
echo "</table>";
mysqli_close($conn);
?>
这是我的前端作品
<font size="4"> Choose an Owner Name</font><br><br>
<form action="flat.php" method="post">
<select name="name">
<?php
$con = mysqli_connect("localhost", "root", "root") or die ("No connection");
mysqli_select_db($con , "flat") or die ("db will not open");
$query = "SELECT distinct OwnerName, FlatCode, Address from FLAT";
/*$query= $_POST ("name")
function change_guery($query)
mysqli_use_result*/
$result = mysqli_query($con, $query) or die("Invalid query");
while($rows = mysqli_fetch_array($result))
{
echo "<option value=\"" . $rows[0] . "\">" . $rows[0] . "</option>";
}
echo "</select>";
mysqli_close($con);
?>
<input type="submit" value="Submit Value">
</form></body></html>
您的 flat.php 代码有问题。您正在通过表单正确发布信息,但您忘记通过 flat.php.
中的 $_POST 接收信息
看下面的代码和里面的注释,应该可以的-
<?php
$n = $_POST["name"];//we receive the name passed by the form
$conn = mysqli_connect("localhost", "root", "root") or die ("No connection");
mysqli_select_db($conn, "flat") or die("db will not open");
$query = "select FlatCode, Address from FLAT WHERE `OwnerName` = '$n' LIMIT 1";//see the changes here
$result = mysqli_query($conn, $query) or die("Invalid query");
echo "<table border='1'><tr><th>modulecode</th><th>studentnum</th></tr>";
$row = mysqli_fetch_array($result);
//as the result will return 1 row only so we dont need while loop here
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td></tr>";
echo "</table>";
mysqli_free_result($result);//dont forget to free result
mysqli_close($conn);
?>
我想从 phpmyadmin 数据库中提取人名并将它们放在 HTML select 框中,当用户从 select 框中选择一个名字时它应该显示table 中该人的数据库详细信息。我似乎无法让它工作,我可以从数据库中获取名称并显示在 select 框中,但是当您单击名称时,它似乎会显示数据库中的每条记录而不仅仅是那个人的那个。我使用 mysql 而不是 mysql。这是我的代码
这是我的后端内容
<?php
$conn = mysqli_connect("localhost", "root", "root") or die ("No connection");
mysqli_select_db($conn, "flat") or die("db will not open");
$query = "select FlatCode, Address from FLAT";
$result = mysqli_query($conn, $query) or die("Invalid query");
echo "<table border='1'><tr><th>modulecode</th><th>studentnum</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td></tr>";
}
echo "</table>";
mysqli_close($conn);
?>
这是我的前端作品
<font size="4"> Choose an Owner Name</font><br><br>
<form action="flat.php" method="post">
<select name="name">
<?php
$con = mysqli_connect("localhost", "root", "root") or die ("No connection");
mysqli_select_db($con , "flat") or die ("db will not open");
$query = "SELECT distinct OwnerName, FlatCode, Address from FLAT";
/*$query= $_POST ("name")
function change_guery($query)
mysqli_use_result*/
$result = mysqli_query($con, $query) or die("Invalid query");
while($rows = mysqli_fetch_array($result))
{
echo "<option value=\"" . $rows[0] . "\">" . $rows[0] . "</option>";
}
echo "</select>";
mysqli_close($con);
?>
<input type="submit" value="Submit Value">
</form></body></html>
您的 flat.php 代码有问题。您正在通过表单正确发布信息,但您忘记通过 flat.php.
中的 $_POST 接收信息看下面的代码和里面的注释,应该可以的-
<?php
$n = $_POST["name"];//we receive the name passed by the form
$conn = mysqli_connect("localhost", "root", "root") or die ("No connection");
mysqli_select_db($conn, "flat") or die("db will not open");
$query = "select FlatCode, Address from FLAT WHERE `OwnerName` = '$n' LIMIT 1";//see the changes here
$result = mysqli_query($conn, $query) or die("Invalid query");
echo "<table border='1'><tr><th>modulecode</th><th>studentnum</th></tr>";
$row = mysqli_fetch_array($result);
//as the result will return 1 row only so we dont need while loop here
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td></tr>";
echo "</table>";
mysqli_free_result($result);//dont forget to free result
mysqli_close($conn);
?>