我有一個從MYSQLi查詢填充的下拉列表。我希望用戶從數據庫中選擇一個選項和相關的值/記錄,並根據FirstName顯示給用戶。顯示數據
我的表沒有一個id列(我沒有做任何。用姓開始)
它顯示的表列標題,但它並沒有動態地從發生在被編碼的數據庫獲取數據下面的[查看代碼]中的while循環。 我花了整整一整天的時間試圖弄清楚這一點。幫助:(
列表代碼
<!DOCTYPE>
<html>
<head>
<title>Update Data</title>
</head>
<body>
<form name="form_update" method="post" action="update_test.php">
<?php
$con=mysqli_connect("localhost","root","","ismat_db");
//============== check connection
if(mysqli_errno($con))
{
echo "Can't Connect to mySQL:".mysqli_connect_error();
}
else
{
echo "Connected to mySQL</br>";
}
//This creates the drop down box
echo "<select name= 'FirstName'>";
echo '<option value="">'.'--- Please Select Person ---'.'</option>';
//$query=mysqli_query($con,"SELECT id,FirstName FROM persons");
$query = mysqli_query($con,"SELECT FirstName FROM persons");
//$query_display = mysqli_query($con,"SELECT * FROM persons");
while($row=mysqli_fetch_array($query))
{
echo "<option value='". $row['id']."'>".$row['FirstName']
.'</option>';
}
echo '</select>';
?> <input type="submit" name="submit" value="Submit"/>
</form>
<br/><br/>
<a href="main.html"> Go back to Main Page </a>
</body>
</html>
查看代碼
<!DOCTYPE>
<html>
<head>
<title>Update Data</title>
</head>
<body>
<?php
$con=mysqli_connect("localhost","root","","ismat_db");
if(mysqli_errno($con))
{
echo "Can't Connect to mySQL:".mysqli_connect_error();
}
if(isset($_POST['FirstName']))
{
$name = $_POST['FirstName'];
$fetch="SELECT Firstname FROM persons WHERE Firstname = '".$name."'";
$result = mysqli_query($con,$fetch);
if(!$result)
{
echo "Error:".(mysqli_error($con));
}
//display the table
echo '<table border="1">'.'<tr>'.'<td align="center">'. 'From Database'. '</td>'.'</tr>';
echo '<tr>'.'<td>'.'<table border="1">'.'<tr>'.'<td>'.'First Name'.'</td>'.'<td>'.'
LastName'.'</td>'.'<td>'. 'Gender' .'</td>'.'<td>'. 'Subject'. '</td>'.'<td>'.'
Hobbies' .'</td>'.'</tr>';
//Supposed to collect data from db-I tried using _array,_assoc instead of _row
//and got mysqli_result()
// requires one parameter given boolean
while($data=mysqli_fetch_row($result))
{
echo ("<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td><td>$data[3]</td> <td>$data[4] </td></tr>");
}
echo '</table>'.'</td>'.'</tr>'.'</table>';
}
?>
我敢肯定,這顯然給你,但是,呃 - 你有什麼問題嗎? – enhzflep
@enhzflep從下拉框中選擇選項時的相關行不會從數據庫中顯示出來。 –