I'm trying hard with displaying details from a selected option in a drop down list for my project.
I have a drop down list which is populated from a MYSQLi query. I want the user to select an option and the values associated pulled from the database and displayed to the user.
The dynamically populated drop down list is for "FirstName" of persons(Table Name) and when a user selects a name from the drop down list I want the record of that person to be to be displayed.
The code below is for dynamically populating the drop down list. The user clicks the button and goes on the next page which should create a table with the results. There is no Error, but no result as required too. I've tried alot now and don't know what to do.
Drop Down 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>
Display View Code
<!DOCTYPE>
<html>
<head>
<title>Update Data</title>
</head>
<body>
<!--<table>
<tr>
<td align="center"> From Database </td>
</tr>
<tr>
<td>
<table border="1">
<tr>
<td>First Name</td>
<td>Last Name </td>
<td> Gender </td>
<td> Subject </td>
<td> Hobbies </td>
</tr>
-->
<?php
$con=mysqli_connect("localhost","root","","ismat_db");
if(mysqli_errno($con))
{
echo "Can't Connect to mySQL:".mysqli_connect_error();
}
//$name = mysqli_real_escape_string($con,$_POST['select']);
// $fetch = mysqli_query($con,"SELECT * FROM persons WHERE FirstName='".$name."'");
// $row_display=mysqli_fetch_assoc($fetch);
if(isset($_POST['FirstName']))
{
$name = $_POST['FirstName'];
//$name = mysqli_real_escape_string($con,$_POST['select']);
//$fetch = "SELECT * FROM persons WHERE FirstName = '".$name."'";
//$fetch="SELECT 'Firstname' FROM persons WHERE Firstname = '".$name."'";
$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>'.'Last Name'.'</td>'.'<td>'. 'Gender' .'</td>'.'<td>'. 'Subject'. '</td>'.'<td>'. 'Hobbies' .'</td>'.'</tr>';
//while($data = mysqli_fetch_row($fetch))
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>';
}
?>
<!--</table>
</td>
</tr>
</table>-->
<br/>
<a href="update.php"> Go back to Main Page </a>
</body>
</html>