steve55 Posted November 20, 2011 Share Posted November 20, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/251509-dynamic-search-form-for-mysql-database/ Share on other sites More sharing options...
Fadion Posted November 21, 2011 Share Posted November 21, 2011 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.'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/251509-dynamic-search-form-for-mysql-database/#findComment-1289862 Share on other sites More sharing options...
steve55 Posted November 21, 2011 Author Share Posted November 21, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/251509-dynamic-search-form-for-mysql-database/#findComment-1290193 Share on other sites More sharing options...
steve55 Posted November 22, 2011 Author Share Posted November 22, 2011 Hi Still stuck on this...If anyone can help that would be ideal... Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/251509-dynamic-search-form-for-mysql-database/#findComment-1290478 Share on other sites More sharing options...
QuickOldCar Posted November 22, 2011 Share Posted November 22, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/251509-dynamic-search-form-for-mysql-database/#findComment-1290531 Share on other sites More sharing options...
steve55 Posted November 22, 2011 Author Share Posted November 22, 2011 Thanks, but that is basically the same code as I have pasted above that isn't working... Quote Link to comment https://forums.phpfreaks.com/topic/251509-dynamic-search-form-for-mysql-database/#findComment-1290546 Share on other sites More sharing options...
QuickOldCar Posted November 23, 2011 Share Posted November 23, 2011 look closer, it's not the same in a few ways Quote Link to comment https://forums.phpfreaks.com/topic/251509-dynamic-search-form-for-mysql-database/#findComment-1290648 Share on other sites More sharing options...
steve55 Posted November 23, 2011 Author Share Posted November 23, 2011 Thanks, tried though and am now looking at a blank white page.... Quote Link to comment https://forums.phpfreaks.com/topic/251509-dynamic-search-form-for-mysql-database/#findComment-1290724 Share on other sites More sharing options...
QuickOldCar Posted November 23, 2011 Share Posted November 23, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/251509-dynamic-search-form-for-mysql-database/#findComment-1290728 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.