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

connect form to mssql database

P: 14
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

Expand|Select|Wrap|Line Numbers
  1. <select name="State">
  2.     <option value="0" selected="selected">Select a State</option>
  3.     <option value="AL">Alabama</option>
  4.     <option value="AK">Alaska</option>
  5.     <option value="AZ">Arizona</option>
  6.     <option value="AR">Arkansas</option>
  7.        etc.....
  8. </select>
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.

Expand|Select|Wrap|Line Numbers
  1. +-----------+-------+---------------+
  2. | stateip_id| state |   user_ip     |
  3. +-----------+-------+---------------+
  4. |      1    | AL    | 67.100.244.74 |
  5. |      2    | AK    | 68.20.131.135 |
  6. |      3    | AZ    | 64.134.225.33 |
  7. +-----------+-------+---------------+
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.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $Server = "00.00.000.000,0000";
  3. $User = "username";
  4. $Pass = "password";
  5. $DB = "dbname";
  6.  
  7. //connection to the database
  8. $dbhandle = mssql_connect($Server, $User, $Pass)
  9.   or die("Couldn't connect to SQL Server on $Server"); 
  10.  
  11. //select a database to work with
  12. $selected = mssql_select_db($DB, $dbhandle)
  13.   or die("Couldn't open database $DB"); 
  14.  
  15. //declare the SQL statement that will query the database
  16. $query = "SELECT stateip_id, state, user_ip ";
  17. $query .= "FROM state_ip ";
  18. $query .= "WHERE state='AK'"; 
  19.  
  20. //execute the SQL query and return records
  21. $result = mssql_query($query);
  22.  
  23. $numRows = mssql_num_rows($result); 
  24. echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>"; 
  25.  
  26. //display the results 
  27. while($row = mssql_fetch_array($result))
  28. {
  29.   echo "<li>" . $row["stateip_id"] . $row["state"] . $row["user_ip"] . "</li>";
  30. }
  31. //close the connection
  32. mssql_close($dbhandle);
  33. ?> 
The problem i have is the WHERE part

Expand|Select|Wrap|Line Numbers
  1. $query .= "WHERE state='AK'"; 
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
Nov 30 '11 #1
Share this question for a faster answer!
Share on Google+

Post your reply

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