0

Hi everyone hope all well. I am needing your valuable expertise in a problem i am having. I will try to explain below.

I have a form and inside of that form i have a "select a state" option:

<select name="State">
<option value="0" selected="selected">Select a State</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
   etc.....
</select>

When the customer selects a state and then clicks the forms submit button, i want them to get taken my mysql database and pull an ip address from the table relevant to what they choose.

I have created a table inside of phpmyadmin with two fields. the first is: state,varchar,30,primary the second is: ip,text

I then imported an csv file which populated it with the following format

state ip

AL 67.100.244.74

AK 68.20.131.135

AK 64.134.225.33

etc.......

So an example would be: customer fills out the form and selects Alabama as a state. They then submit the form and the form connects to the database where it sees the state Alabama (AL). It then collects the ip from the ip section releated to the ALbama section, and submits that to my email address along with the rest of the form information (name,email,etc...). It also needs to randomly choose an ip from Alabama because in the database i have Alabama (AL) multiple times, so it just chooses anyone of the Alabama ip's

I hope i am making sense

If you know how to do this or where i can find the info it would be much appreciated

Thanks for all your help

Ali


Thanks for all your help and sorry in the delay. Whenever i add that code to the page i get this error

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/b/o/l/boldbaba406/html/1stop/1vince/test1.php on line 3

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/content/b/o/l/boldbaba406/html/1stop/1vince/test1.php on line 3

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/content/b/o/l/boldbaba406/html/1stop/1vince/test1.php on line 4

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/b/o/l/boldbaba406/html/1stop/1vince/test1.php on line 5

I am probably doing something really stupid, but i can not see what. Any help would be great

Ali

3
  • What is your CGI program doing so far? Commented Oct 24, 2011 at 13:24
  • 1
    Not an answer, just a general point, but... you say I have created a table ... with two fields - have you created a third, auto-incrementing index field (primary key) as well? As a general rule every table should have one of these, no matter how simple - there are some exceptions, but they are few and far between... Commented Oct 24, 2011 at 13:24
  • On your update: you have no existing connection with your database.. Without a database connection you cannot execute queries, naturally. Take a look at this nl3.php.net/manual/en/function.mysql-connect.php -- See my updated post Commented Nov 7, 2011 at 15:17

2 Answers 2

1

Assuming your table looks something like this:

+-------+---------------+
| state |      ip       |
+-------+---------------+
| AL    | 67.100.244.74 |
| AK    | 68.20.131.135 |
| AZ    | 64.134.225.33 |
+-------+---------------+

Your script could be like this:

<select name="State">
<option value="0" selected="selected">Select a State</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
   etc.....
</select>

<?php
$state = $_POST['State']; // User selected Alaska (AK) so $_POST['State'] = 'AK'
$query = "SELECT state, ip FROM ipaddresses WHERE state='" . mysql_real_escape_string($state) . "'";
$link_resource = mysql_connect($host, $username, $password);
$db = mysql_select_db($database);
$result = mysql_query($query, $link_resource);
$row = mysql_fetch_assoc($result);

// ip-address belonging to Alaska (AK) turns out to be 68.20.131.135 as it's stored in the same row as AK
?>

The IP-address belonging to AK is now stored in $row['ip']. The other form fields can be accessed by usign the $_POST array, e.g. $_POST['customer_name'] if your form has an input field by the name of 'customer_name'.

UPDATE: Added database connection. This is just a really basic example, you should do some research yourself on database connections and queries. I advise you to start learning MySQLi (MySQL Improved Extension) immediately instead of the 'old & ordinary' MySQL.

Sign up to request clarification or add additional context in comments.

Comments

0

You can get MySQL to return a random row like this:

// Get the state that the user selected
$selectedState = mysql_real_escape_string($_REQUEST['State']);

// Build a query
$query = "SELECT ip
          FROM table_name
          WHERE state = '$selectedState'
          ORDER BY rand()
          LIMIT 1";

// Execute it
$result = mysql_query($query);

// Fetch the result as an array
$row = mysql_fetch_assoc($result);

// The IP address you want is now stored in $row['ip']

The important bit is ORDER BY rand() LIMIT 1 - this is the part that gets 1 random matching row, instead of all rows.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.