Fabrizzio PHP Posted August 26, 2009 Share Posted August 26, 2009 Hi, I’m a PHP beginner learning through online tutorials and need some help: On my index.php file I’ve got a dynamic drop down list which displays the “university” names and when the form is submitted, it should output from my databse: the “content”, “post_code” and “borough” in my result.php. Unfortunately, all I’ve managed to display is the name of the university which is assigned to the "select value". Can I please have assistance on how to output all the database info based on the “select” university? I’ve got a database called "map" with the following table: id university position visible content post_code borough My index.php file has the following code: <?php // Connect database mysql_connect("localhost","root","xxx"); mysql_select_db("map"); // If submitted, check the value of "select". If its not blank value, get the value and put it into $select. if(isset($select)&& $select!=""){ $select=$_GET['select']; } ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <form id="form1" name="form1" method="post" action="result.php""> Search by borough : <select name="select"> <option value="">--- Select ---</option> <?php // Get records from database (table "subjects"). $list=mysql_query("SELECT * FROM subjects ORDER BY id ASC"); // Show records by while loop. while($row_list=mysql_fetch_assoc($list)){ ?> <option value="<?php echo $row_list['university']; ?>" <?php if($row_list['id'] == '$select'){ echo "selected"; } ?>><?php echo $row_list['university']; ?></option> <?php // End while loop. } ?> </select> <input type="submit" name="Submit" value="select" /> </form> <?php mysql_close(); ?> </p> </body> </html> //My result.php contains the following: <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <html> <head> </head> <body> The University is: <?php echo $_POST["select"]; ?> The Postcode is: The content (university description is): </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/171957-solved-outputting-several-elements-from-a-database-with-a-php-form/ Share on other sites More sharing options...
ignace Posted August 26, 2009 Share Posted August 26, 2009 $row_list['id'] == '$select' should be $row_list['id'] == $select For the query part: SELECT * FROM map WHERE university = $_POST['select'] Quote Link to comment https://forums.phpfreaks.com/topic/171957-solved-outputting-several-elements-from-a-database-with-a-php-form/#findComment-906737 Share on other sites More sharing options...
Fabrizzio PHP Posted August 27, 2009 Author Share Posted August 27, 2009 Hi Ignace, I've changed: $row_list['id'] == '$select' [code] to $row_list['id'] == $select[/code] and I get this error message: Notice: Undefined variable: select in C:\wamp\www\map\index.php on line 28>UCLA (all along the dropdown menu). <?php $query= ("SELECT * FROM subjects WHERE university = $_GET["select"]"); $row_list=mysql_fetch_assoc($query); ?> The borough is: <?php echo $row_list['borough']; ?> And I get the following error: Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\map\myform.php on line 10 Am I writing the code right?? Quote Link to comment https://forums.phpfreaks.com/topic/171957-solved-outputting-several-elements-from-a-database-with-a-php-form/#findComment-907477 Share on other sites More sharing options...
gaza165 Posted August 27, 2009 Share Posted August 27, 2009 change <?php $query= ("SELECT * FROM subjects WHERE university = $_GET["select"]"); $row_list=mysql_fetch_assoc($query); ?> The borough is: <?php echo $row_list['borough']; ?> to <?php $select = $_GET['select']; $query= ("SELECT * FROM subjects WHERE university = '$select' "); $row_list=mysql_fetch_assoc($query); ?> The borough is: <?php echo $row_list['borough']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/171957-solved-outputting-several-elements-from-a-database-with-a-php-form/#findComment-907501 Share on other sites More sharing options...
Fabrizzio PHP Posted August 27, 2009 Author Share Posted August 27, 2009 Hi gaza165, I've written your code and now I get the following error message: Notice: Undefined index: select in C:\wamp\www\map\myform.php on line 13 Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\map\myform.php on line 15 The borough is: You have chosen: Westminster I'm also still wondering the correct way to write: <?php if($row_list['id'] == '$select') or <?php if($row_list['id'] == $select) and if this could have to do with the errors display? Quote Link to comment https://forums.phpfreaks.com/topic/171957-solved-outputting-several-elements-from-a-database-with-a-php-form/#findComment-907626 Share on other sites More sharing options...
gaza165 Posted August 27, 2009 Share Posted August 27, 2009 Is $select a integer or a string?? If it is an integer do this.... <?php $select = $_GET['select']; $query= ("SELECT * FROM subjects WHERE university = $select "); $row_list=mysql_fetch_assoc($query); ?> The borough is: <?php echo $row_list['borough']; ?> what is on line 13 Quote Link to comment https://forums.phpfreaks.com/topic/171957-solved-outputting-several-elements-from-a-database-with-a-php-form/#findComment-907659 Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 Is $select a integer or a string?? If it is an integer do this.... <?php $select = $_GET['select']; $query= ("SELECT * FROM subjects WHERE university = $select "); $row_list=mysql_fetch_assoc($query); ?> The borough is: <?php echo $row_list['borough']; ?> what is on line 13 That is not to what the error is referring. 'select' does not exist in the _GET array. Quote Link to comment https://forums.phpfreaks.com/topic/171957-solved-outputting-several-elements-from-a-database-with-a-php-form/#findComment-907662 Share on other sites More sharing options...
Fabrizzio PHP Posted August 27, 2009 Author Share Posted August 27, 2009 Line 13 is: $select = $_GET['select']; I've tried the other way and I get the following error message: Notice: Undefined index: select in C:\wamp\www\map\myform.php on line 13 Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\map\myform.php on line 15 The borough is: You have chosen: Westminster Quote Link to comment https://forums.phpfreaks.com/topic/171957-solved-outputting-several-elements-from-a-database-with-a-php-form/#findComment-907695 Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 Notice: Undefined index: select in C:\wamp\www\map\myform.php on line 13 You need select=value in your url. Quote Link to comment https://forums.phpfreaks.com/topic/171957-solved-outputting-several-elements-from-a-database-with-a-php-form/#findComment-907715 Share on other sites More sharing options...
Fabrizzio PHP Posted August 27, 2009 Author Share Posted August 27, 2009 Hi Ignace, You need select=value in your url. Sorry but I'm a PHP beginner and I'm not really sure what to write. Shouldn't $select = $_GET['select']; give select a value in my URL? Also where you suggested me to change ($row_list['id'] == '$select') to ($row_list['id'] == $select) I get Notice: Undefined variable: select in C:\wamp\www\map\index.php on line 28>UCLA (all along the dropdown menu). Quote Link to comment https://forums.phpfreaks.com/topic/171957-solved-outputting-several-elements-from-a-database-with-a-php-form/#findComment-907767 Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 Notice: Undefined index: select in C:\wamp\www\map\myform.php on line 13 Becuase you are missing select in your url http://domain/page.php?select=something Notice: Undefined variable: select in C:\wamp\www\map\index.php on line 28>UCLA (all along the dropdown menu). Because you need to add: $select = $_GET['select']; before: ($row_list['id'] == $select) Quote Link to comment https://forums.phpfreaks.com/topic/171957-solved-outputting-several-elements-from-a-database-with-a-php-form/#findComment-907779 Share on other sites More sharing options...
Fabrizzio PHP Posted August 27, 2009 Author Share Posted August 27, 2009 Hi Ignace, Thanks, I think I'm getting there! I've given select a value in my URL: action="myform.php?select=<?php echo urlencode($select['id']); I'm not sure but still having an error message: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\map\myform.php on line 15 The borough is: You have chosen: Westminster Is there something I'm writing wrong in the select URL? So far this is my code for form.php if(isset($select)&& $select!=""){ $select=$_GET['select']; } ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <form id="form1" name="form1" method="post" action="result.php?select=<?php echo urlencode($select['id']); ?>"> Search by borough : <select name="select"> <option value="">--- Select ---</option> <?php // Get records from database (table "subjects"). $list=mysql_query("SELECT * FROM subjects ORDER BY id ASC"); // Show records by while loop. while($row_list=mysql_fetch_assoc($list)){ ?> <?php $select = $_GET['select'];?> <option value="<?php echo $row_list['borough']; ?>" <?php if($row_list['id'] == $select){ echo "selected"; } ?>><?php echo $row_list['borough']; ?></option> <?php // End while loop. } ?> </select> <input type="submit" name="Submit" value="select" /> </form> And my result.php <body> <?php $select = $_GET['select']; $query= ("SELECT * FROM subjects WHERE borough = $select "); $row_list=mysql_fetch_assoc($query); ?> The University is: <?php echo $row_list['universty']; ?><br /> The borough is: <?php echo $_POST["select"]; ?> </body> Quote Link to comment https://forums.phpfreaks.com/topic/171957-solved-outputting-several-elements-from-a-database-with-a-php-form/#findComment-907817 Share on other sites More sharing options...
mikesta707 Posted August 27, 2009 Share Posted August 27, 2009 assuming $select is a string, change your query to "SELECT * FROM subjects WHERE borough = '$select' " also, $query isn't a valid mysql_resource because well... it isn't a mysql_resource, its a string. you have to preform a mysql_query with that string in order to get data from the table so chagne $query to $query= mysql_query("SELECT * FROM subjects WHERE borough = '$select' "); Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/171957-solved-outputting-several-elements-from-a-database-with-a-php-form/#findComment-907823 Share on other sites More sharing options...
Fabrizzio PHP Posted August 27, 2009 Author Share Posted August 27, 2009 Hi mikesta707, I've made the change and I don't get an error message but the following: The borough is: You have chosen: Westminster I'm not sure if my URL select value has to do with it: <form id="form1" name="form1" method="post" action="myform.php?select=<?php echo urlencode($select['id']); ?>"> Quote Link to comment https://forums.phpfreaks.com/topic/171957-solved-outputting-several-elements-from-a-database-with-a-php-form/#findComment-907831 Share on other sites More sharing options...
mikesta707 Posted August 27, 2009 Share Posted August 27, 2009 Problem is you are using post, and not get. Values passed via the URL are stored in the $_GET array. so change the following: The borough is: <?php echo $_POST["select"]; ?> to The borough is: <?php echo $_GET['select']; ?> and it should work Quote Link to comment https://forums.phpfreaks.com/topic/171957-solved-outputting-several-elements-from-a-database-with-a-php-form/#findComment-907838 Share on other sites More sharing options...
Fabrizzio PHP Posted August 28, 2009 Author Share Posted August 28, 2009 Hi Mikesta707, Thanks! I tweaked your code and it actually work! For anyone interested on how to post different values from a PHP Form using a drop down list here is the code: I’ve got a database called "XXX" with the following table: id university position visible content post_code borough form.php <?php // Connect database mysql_connect("localhost","root","YOURPASSWORD"); mysql_select_db("YOUR DATABASE NAME"); // If submitted, check the value of "select". If its not blank value, get the value and put it into $select. if(isset($select)&& $select!=""){ $select=$_GET['select']; } ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <form id="form1" name="form1" method="post" action="result.php?select=<?php echo urlencode($select['id']); ?>"> Search by borough : <select name="select"> <option value="">--- Select ---</option> <?php // Get records from database (table "subjects"). $list=mysql_query("SELECT * FROM subjects ORDER BY id ASC"); // Show records by while loop. while($row_list=mysql_fetch_assoc($list)){ ?> <?php $select = $_GET['select'];?> <option value="<?php echo $row_list['university']; ?>" <?php if($row_list['id'] == $select){ echo "selected"; } ?>><?php echo $row_list['universtity']; ?></option> <?php // End while loop. } ?> </select> <input type="submit" name="Submit" value="select" /> </form> <?php mysql_close(); ?> </p> </body> </html> result.php <?php // Connect database mysql_connect("localhost","root","oizzirbaf"); mysql_select_db("map"); ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <?php $select = $_POST['select']; $query= mysql_query("SELECT * FROM subjects WHERE borough = '$select' "); $row_list=mysql_fetch_assoc($query); ?> You have chosen: <?php echo $_POST["select"]; ?> University!<br /> The borough is: <?php echo $row_list['borough']; ?><br /> </body> </html> THANKS TO EVERYONE YOU WERE ALL OF GREAT HELP! Quote Link to comment https://forums.phpfreaks.com/topic/171957-solved-outputting-several-elements-from-a-database-with-a-php-form/#findComment-908215 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.