Echo'Hello程序员';
我正在摸一下下拉菜单。它们应该显示ename和mid行中的所有字符串。但是这没有发生,下拉列表只显示每行的一个结果。实际行中有多个测试数据字符串。
我在这里有一些代码,也许你可以伸出援助之手。让我解释一下。
首先,这些是dbme类的方法。为了保持混乱,第二个函数是完全相同的,除了getResult()的SQL查询明显不同(SELECT *来自成员而不是memberevent)
function openDB() {//creating database connection
$conn = mysqli_connect("localhost", "root", "", "mydb");
if (!$conn) {
$this->error_msg = "connection error could not connect to the database:! ";
return false;
}
$this->conn = $conn;
return true;
}
function getResult($sql){
$result = mysqli_query($this->conn , "SELECT * from memberevent" );
if ($result) {
return $result;
} else {
die("SQL Retrieve Error: " . mysqli_error($this->conn));
}
}
其次,这是网络侧数据。
$db1 = new dbme();
$db1->openDB();
$sql="select mid from member";
$result=$db1->getResult($sql);// get the ids from the tables for the select
$sql1="select ename from event";
$result1=$db1->getResult($sql1);// get the ids from the tables for the select
if (!$_POST) //page loads for the first time
{
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" onsubmit="return validateForm( );">
Select Member ID: <select name="mid">
<?php
while($row = mysqli_fetch_assoc($result))
echo "<option value='{$row['mid']}'>{$row['mid']} </option>";
?>
</select>
<br />
Select Event name : <select name="ename">
<?php
while($row = mysqli_fetch_assoc($result1))
echo "<option value='{$row['ename']}'>{$row['ename']} </option>";
?>
</select>
<br />
我怀疑变量是否已经过度写入,或者某处需要额外的循环。但我不太确定,所以我发帖提出这个问题。
感谢。
答案 0 :(得分:1)
您需要在getResults()
函数中迭代结果,否则您将始终只获得一行。
答案 1 :(得分:0)
您正在传递SQL但从未使用它:
更改
function getResult($sql){
$result = mysqli_query($this->conn , "SELECT * from memberevent" );
要
function getResult($sql){
$result = mysqli_query($this->conn , $sql );