473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

PHP/MySQL lookup location/country (beginner)

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
6 2147
ronverdonk
4,258 Expert 4TB
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
202 100+
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
104 100+
Here is a thread for you:

http://www.thescripts.com/forum/thread545390.html
Dec 6 '06 #4
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
4,258 Expert 4TB
"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
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

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

Similar topics

2
by: Jon Dellaria | last post by:
I have been using MySql as the database using JSP's and JavaBeans but recently I have wanted to start using the database connection pooling mechanism built into TomCat. I think I am having a...
4
by: www | last post by:
I want to know if there is a free service or database available from where I can get the country information if I pass it the IP address. If been to a site where you can do a free lookup, but I...
0
by: 143.182.124.4 [Shameer Balasingam] | last post by:
There is a database to lookup all IP address space to country. Pretty cool... I've used it for my web application and I believe it can be used by C too. http://www.location.com.my/products.asp...
0
by: odysseyphotography | last post by:
Hi, I'm trying to build a simple search for a database of jobs. Users can search using three groups of critera: 1) Area (check boxes) Buying and merchandising Design Executive
5
by: Mike | last post by:
Hi all, I'm developing an ASP.NET 2.0 site that require to lookup the visitor's country based on the IP. Is there any free, simplest way to implement (using a web service, maybe) this thing? ...
2
by: Boujii | last post by:
Greetings, I have been attempting to make a drop down menu of countries. From this menu I wish to create a variable in order to INPUT into mysql database. I have no trouble making the drop down menu,...
2
by: Boujii | last post by:
<html> <head> <title>Add New MySQL User</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <? if(isset($_POST)) {
1
by: Scott McFadden | last post by:
Using .NET, what are some good ways to determine which country a given IP the request IP address resides in? thanks scottm
1
George Lft
by: George Lft | last post by:
ok, first of all, i built my register page using dreamweaver tool which the codes haven been out of control. Now i'm thinking that turning over everything - by using this another set of codes. And...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.