By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,309 Members | 739 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,309 IT Pros & Developers. It's quick & easy.

PHP/MySQL lookup location/country (beginner)

P: 3
Hey there =)

Im trying to build a little program that goes something like this.

1. user types in location
2. hit submit
3. page tells user what country they are in

It all seems very simple but as a beginner with php i need all the help i can get.

I have created a connection that i can include in my pages.
Any help would be much appreciated, or even links to useful tutorials.

Cheers,
kind regards, gillian
Dec 3 '06 #1
Share this Question
Share on Google+
6 Replies

ronverdonk
Expert 2.5K+
P: 4,258
Show us what code you have made until now. Then we can help you further develop it until you get what you want to accomplish.

Ronald :cool:
Dec 3 '06 #2

cassbiz
100+
P: 202
Here is a pretty good tutorial with quite a few examples on creating, updating and retrieivng information using PHP MySQL

http://www.php-mysql-tutorial.com/index.php
Dec 5 '06 #3

moishy
100+
P: 104
Here is a thread for you:

http://www.thescripts.com/forum/thread545390.html
Dec 6 '06 #4

P: 3
Show us what code you have made until now. Then we can help you further develop it until you get what you want to accomplish.

Ronald :cool:
[PHP]<html>
<head><title> Whereabouts</title></head>

<body>
<?php
include('my_conn.php');

/*create form*/
echo "<form action='whereabouts.php' method='post'>\n";
echo "<table cellpadding='5' border='1'>\n";
echo "<input type='text' name='location'>\n";
echo "<input type='submit' value='submit'>
</form>\n\n";

/*Select all catergories from country table*/
$query = "SELECT * FROM country";
$result = mysql_query($query);

while($row = mysql_fetch_array($result))
{
echo "COUNTRY ID: {$row['countryID']} <br>" .
"COUNTRY : {$row['country']} <br><br>";
}

?>
</body>
</html>[/PHP]

The code is rather basic still, but i did manage to retrieve data and display it on a page with my lil' sql statement, so proud of myself, hehe.

What i really want with my sql statement is

if 'textfield' = a location in the location table
match the countryID.location with countryID.country
then display
'textfield' is in 'country'

my database is like the following

table location
-------------------
location
countryID (foreign key)

table country
-------------------
country
countryID (foreign key)

any help will be grand =)

cheers,
kind regards, gillian
Dec 10 '06 #5

ronverdonk
Expert 2.5K+
P: 4,258
"The title of this thread has been changed to make the problem clearer to experts and others searching for similar threads."

MODERATOR
This will do it. I changed the order of the code:
1. first check if this script is called due to a submit, if so:
a. connect to MySQL server and db
a select with the appropriate WHERE attribs
b. display the found data
2. (re)display the form

[php]<?php
// check if form is posted
if (isset($_POST['submit']) AND isset($_POST['location'])) {

// sanitize the POSTed value
$location = strip_tags(trim($_POST['location']));

// connect to server and db
include('my_conn.php');

// Select all categories from country table
$query = "SELECT * FROM country, location
WHERE location.location = '$location'
AND location.countryID = country.countryID";
$result = mysql_query($query)
or die("SELECT error: ".mysql_error());

// retrieve and display all results
while($row = mysql_fetch_array($result)) {
echo "COUNTRY ID: {$row['countryID']} <br>" .
"COUNTRY : {$row['country']} <br><br>";
} // END While

} // END if(isset ....

// re-display the form
echo "<html><head><title> Whereabouts</title></head><body>";
/*create form*/
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>\n";
echo "<input type='text' name='location'>\n";
echo "<input type='submit' name='submit' value='submit'>
</form>\n\n";
echo "</body></html>";
?>[/php]
Have fun with it.
If you consider to make this into a full-fledged application, it will be worth to look at Ajax to do the asynch table lookup and display without doing a page refresh.

Ronald :cool:
Dec 10 '06 #6

P: 3
Thank you so much, that worked a treat. =)

Wondering one last thing.
When i use it in IE, it only works if you press go, do i have to add extra code for it to allow me just to press enter?

Enter works fine in firefox.

cheers,
kind regards, gillian

ps. it resides here . . . Whereabouts
Dec 11 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.