I have a drop down list which is populated from a MYSQLi query. I want the user to select an option and the values/record associated pulled from the database and displayed to the user based upon FirstName.
my table does not have an id column (I didnt make any. Starts with FirstName)
It displays the table with column titles but it does not fetch the data dynamically from the db which happens to be coded in the while loop in [view code] below. I've spent my whole day and whole night trying to figure this out. Help :(
list code
<!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>
View Code
<!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>';
}
?>