September 19, 200817 yr Hi to all I've trying to solve this issue for a while, and cannot find where the problem is. This is what i'm stuck into: I have a simple database table named 'cities' with two rows: 'id_city' and 'city_name'. I'm trying to make a search form, in which the name of the cities are displayed in a drop down. So search.php is something like this: <?php mysql_select_db(city_app,$con); $qry="SELECT * FROM `cities` ORDER BY `city_name` ASC"; $res=mysql_query($qry) or die(mysql_error()); ?> <form action="search_results.php" method="get"> <select name="city" > <option value="">---Choose a city---</option> <?php while($row=mysql_fetch_assoc($res)){ echo '<option value="'.$row['id_city'].'">'.$row['city_name'].'</option>'; } ?> <input type="submit" name="submit" value="Submit" /> </form> As you can see, the form passes the value id_city to the search_results.php, where the actual problem is ( i guess ). Now the search_results.php: <?php if(isset($_GET['submit'])){ $city_name=$_GET['city']; echo $city_name // gives me the value of id_city $kv="SELECT `city_name` FROM `cities` WHERE `id_city`=`$city_name`"; $rez=mysql_query($kv) or die(mysql_error()); echo $rez; } ?> What I'm trying to do is to diplay the city name instead the value of the id_city, but cannot figure how. If I echo the $city_name, i get the value of the id_city identical to echo '<option value="'.$row['id_city'].'">....... in the search form. Can anyone guide me to this noobish problem i have? Thanks in advance.
September 19, 200817 yr In the search_results.php file you should use mysql_fetch_row (not echo $rez;).
September 19, 200817 yr You have: WHERE `id_city`=`$city_name`"; your comparing the ID to the name when your form submits the ID. Try this: <?php if( isset($_GET['submit']) ) { $id_city = mysql_real_escape_string($_GET['city']); $kv = "SELECT `city_name`, `id_city` FROM `cities` WHERE `id_city`=`$id_city`"; $rs = mysql_query($kv) or die(mysql_error()); echo mysql_result($rs, 0); } ?>
September 19, 200817 yr Author thanks for this, guys. both mysql_fetch_row and mysql_result gives me the name of the city selected in the drop down, which is what i wanted to achieve. thanks again the only problem is that when i use single quotes in the query it gives an SQL error, which is pretty strange since as much as i know, using them in a query prevents (together with mysql_real_escape_string) from SQL injection
Create an account or sign in to comment