Hey Everyone hope all ok. I am needing your expertise.
I have an html form and inside of it i have a drop down option to choose a state
Any-time the customer selects a a state and submits the form it goes to my mssql database and pulls an ip address releavant to the staten they choose in the html form.
So for example, lets say they choose Alabama (AL), when they submit the form i want the code to connect to the php file and then show the ip address releavant to the state, in this case (AL). For each state i have 200 different ip addresses, so i want it to randomly choose and ip for the state choosen.
I found some php code and have tested it with my details and it is connecting to the database fine.
The problem i have is the WHERE part
If i do like i have above and add a AK, or AL in the '' section it displays all ip address that are associated with AL or AK. What i want it to do is recognise the state choosen from the form and then only display one ip address randomly choosen according to the state
I am just not sure what to add to that WHERE part to make it all work and have it randomly select an ip from the state
Any help would be much appreciated.
Thanks Everyone
I have an html form and inside of it i have a drop down option to choose a state
Code:
<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>
Code:
+-----------+-------+---------------+ | stateip_id| state | user_ip | +-----------+-------+---------------+ | 1 | AL | 67.100.244.74 | | 2 | AK | 68.20.131.135 | | 3 | AZ | 64.134.225.33 | +-----------+-------+---------------+
I found some php code and have tested it with my details and it is connecting to the database fine.
Code:
<?php
$Server = "00.00.000.000,0000";
$User = "username";
$Pass = "password";
$DB = "dbname";
//connection to the database
$dbhandle = mssql_connect($Server, $User, $Pass)
or die("Couldn't connect to SQL Server on $Server");
//select a database to work with
$selected = mssql_select_db($DB, $dbhandle)
or die("Couldn't open database $DB");
//declare the SQL statement that will query the database
$query = "SELECT stateip_id, state, user_ip ";
$query .= "FROM state_ip ";
$query .= "WHERE state='AK'";
//execute the SQL query and return records
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
//display the results
while($row = mssql_fetch_array($result))
{
echo "<li>" . $row["stateip_id"] . $row["state"] . $row["user_ip"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>
Code:
$query .= "WHERE state='AK'";
I am just not sure what to add to that WHERE part to make it all work and have it randomly select an ip from the state
Any help would be much appreciated.
Thanks Everyone