Jump to content

Dynamic search form for mysql database


steve55

Recommended Posts

I'm using some code to create a select menu of a fieldname of data  I have in a mysql database :

<?php
mysql_connect('localhost' , 'dbname', 'password');
mysql_select_db('dbname');
$result=mysql_query("SELECT * FROM Persons");
if(mysql_num_rows($result)>0)
{
?>
<select name="Persons">
<?php
while($rows=mysql_fetch_array($result)){
?>
<option value="<?php echo $rows['id']; ?>"> <?php echo $rows['FirstName']; ?></option>
<?php
}
?>
</select>

What I would like to do is to elevate this into a jump menu form so that if the user selects an item from my form they are taken to a results page showing the full row of data from the database. Basically a search form containing items from the database they can choose to see more details on.

 

eg. In my example you select a persons name and then you are taken to a results page which displays the details of that person from the database.

 

Problem is I don't know how to do this and have been trawling around for a couple of days to find a solution (sorry I'm new to php). I would appreciate some help or a working example would be great of :

 

1/ A working dynamic jump menu

2/ The page that would process the form

3/ The results page displaying the data I have selected.

 

 

Thank you for your time...

 

 

Link to comment
Share on other sites

The <select> dropdown looks correct. You have set the ID as the value and you're to go with form validation and processing. The code below will be run once the form has been submitted, will get the person's information from the database (based on the selected ID) and print it out. Easy enough.

 

<?php
if (isset($_POST['Persons'])) {
//get the selected user ID
$id = (int) $_POST['Persons'];

$results = mysql_query("SELECT name, surname, age, email, phone FROM persons WHERE id=$id");
if (mysql_num_rows($results)) {
	$values = mysql_fetch_assoc($results);

	$name = $values['name'];
	$surname = $values['surname'];
	//and so on for every field

	echo "<b>Name: </b> $name<br />";
	echo "<b>Surname: </b> $surname<br />";
	//and so on for every field
} else {
	echo 'No user found.';
}
}
?>

Link to comment
Share on other sites

Thanks - How do I add the form field to my code though (sorry)

 

I'm trying this, but it isn't working :

 

<form name=persons method=post action=process.php >

<select name="Persons">

<?php

while($rows=mysql_fetch_array($result)){

?>

<option value="<?php echo $rows['id']; ?>"> <?php echo $rows['FirstName']; ?></option>

<?php

}

?>

</select>

</form>

Link to comment
Share on other sites

A form structure goes along the line of this.

 

<form action="process.php" method="post">
<select name="Persons">
<?php
while($rows=mysql_fetch_array($result)){
?>
<option value="<?php echo $rows['id']; ?>"><?php echo $rows['FirstName']; ?></option>
<?php
}
?>
</select>
<input type="submit" value="Go!" />
</form>

Link to comment
Share on other sites

place this the top of your page to see errors

 

<?php
ini_set('display_errors',1); 
error_reporting(E_ALL);
?>

 

you can also try posting your entire code here to try to help more

the mysql connect code i never saw ...exclude your credentials

 

show your current form,process/results pages please

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.