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>

The commenting on the view display code is all the "tries" that I have done. I just started to learn php and Im working on my project that is why I dont know much just learning on the go

I use the older PHP - however, I think this is sort of what you need to change your code to (just with MySQLi):

I believe you have your script almost there - i believe that there are issues with the "receiving" form.
This is one of my code:

    //Link to the database (made before this part of code)
    $Link = mysql_connect($DBHost,$DBUser,$DBPassword);
    //Write the query
    $Query = "SELECT * FROM $Table_2 WHERE id=".$idNumber;
    //Run the query, and provide the result into the variable.
    $Result = mysql_query ($Query, $Link) or die (mysql_error($Link));
    $data2 = mysql_fetch_array($Result);
    ?>

    <input type="text" class="inset" name="title" id="title" accesskey="3" title="Enter a title" value="<?php echo $data2['title']?>"/>

Is that the sort of thing your wanting?

Hi Maharrington - Thanks for replying :) - I kinda solved it.
The problem was that my table in the database did not have any id column, so when i made the drop down list and like normally everyone does i gave the <option> tags attribute value to be id - in the line

 echo "<option value='". $row['id']."'>".$row['FirstName']
 .'</option>';

which did work then but later on created problems, So I changed the $row['id'] to $row['FirstName'] so now both of them had 'FirstName'

The second thing that was wrong was in my query

$fetch="SELECT Firstname FROM persons WHERE Firstname = '".$name."'";

the query in Display View Code only selects FirstName, but the table tried to print LastName, Gender, Subject, and Hobbies(Which were all columns in the table and called or dynamically generated in the while loop of the display view code snippet)

So I fixed that by changing it to

$fetch="SELECT Firstname,LastName,Gender,Subject,Hobbies FROM persons WHERE Firstname = '".$name."'";

and it was all correct.
**the result ** A drop down list display Firstnames of people as options and upon selecting a person and pressing the submit button, their associated row from the table is displayed.

Well Done!

Remember that it is important always to have a unique primary key in every record in a table, therefore ensure that you use an id field (you could've used this is your query).

If you're selecting all fields from a table in a query, then you can use * (means all).
eg. "SELECT * FROM persons WHERE Firstname='".$name."'";

If this question is solved, please mark it as so using the button below.

I'm trying hard with displaying details from a selected option in a drop down .I want the user to select an option firstname in userprofile table and the values associated pulled from the database and displayed to the user. The table consists of firstname,lastname,gender,department.I need the html and php codes to do the project.please help me in this.

@ganeshvasanth please open your own thread and share what you have done, so we can understand where is the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.