Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Displaying row from a table (php)

Featured Replies

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.

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);
}
?>

  • 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

  • 3 weeks later...

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.