briankopy Posted March 21, 2010 Share Posted March 21, 2010 Thanks in advance for reading/any help. I'm basically trying to create a customer request form (.html page) that will activate a .php file that will pull customers from a MySQL database based on their state that the user enters and on a new page display the customers names and phone numbers. I'm a noob, There is probably an elegant solution that I can't figure out, I may have to start over etc. The following is the initial html code that allows the user to type in a two letter state abbreviation and click a button. On that click it will send the state and run "customer_display.php". <html> <head> <title> </title> </head> <body> <form action="customer_display.php" method="post" name="customer_search"> <table width="25%" border="0" cellspacing="2" cellpadding="2" align="center"> <tr> <td>Customer Records</td> <td> </td> </tr> <tr> <td>Single State (optional):</td> <td><input type="text" name="state" size="2" maxlength="2" /></td> </tr> <tr> <td><input type="submit" value="Submit Query" /></td> <td> </td> </tr> </table> </form> </body> </html> Here is all I have for customer_display.php It assigns the state data to a varaible called state... <html><head><title></title></head> <body> <?php $state = $_POST['state']; ?> </body> </html> I have a MySQL database called "database1" with a table called "customertable". The fields that I want to display on a new page after the user clicks the button are firstname lastname area_code phone_number and I would like to give the user the option to pull these records by state. Again any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/196047-phpmyadmin-mysql-database-query/ Share on other sites More sharing options...
MadTechie Posted March 21, 2010 Share Posted March 21, 2010 okay I'll give you a kick in the right direction Your need to open a connection to the database, then query the table, then display the results here is a basic example (untested) <?php $state = $_POST['state']; //OPEN CONNECTION //UPDATE to correct username and password for your database $conn = mysql_connect("localhost", "mysql_user", "mysql_password"); if (!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db("database1")) { echo "Unable to select database1: " . mysql_error(); exit; } //QUERY database //select every field, in customertable that has the state selected $sql = "SELECT * FROM customertable WHERE state='$state' "; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } //DISPLAY results while ($row = mysql_fetch_assoc($result)) { echo $row["firstname"]; echo " "; //space echo $row["lastname"]; //add others echo "<BR />"; } I hope that gets you started Quote Link to comment https://forums.phpfreaks.com/topic/196047-phpmyadmin-mysql-database-query/#findComment-1029753 Share on other sites More sharing options...
briankopy Posted March 21, 2010 Author Share Posted March 21, 2010 Thanks a lot, looks good, but... I received this error Warning: mysql_connect() [function.mysql-connect]:Access denied for user 'user1'@'localhost' (using password: YES) I have my username (user1) and password set up but it is denying me access to the database, any ideas. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/196047-phpmyadmin-mysql-database-query/#findComment-1029772 Share on other sites More sharing options...
briankopy Posted March 21, 2010 Author Share Posted March 21, 2010 nevermind on that particular issue, case sensitive user name, my bad... Quote Link to comment https://forums.phpfreaks.com/topic/196047-phpmyadmin-mysql-database-query/#findComment-1029775 Share on other sites More sharing options...
briankopy Posted March 21, 2010 Author Share Posted March 21, 2010 I really appreciate the kick in the right direction... I have the records all displaying, is there a way to say only show the records with the 'state' field of NY or FL etc. I understand if I've hit my help limit... Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/196047-phpmyadmin-mysql-database-query/#findComment-1029778 Share on other sites More sharing options...
abazoskib Posted March 21, 2010 Share Posted March 21, 2010 madtechie already posted the answer to your question: //select every field, in customertable that has the state selected$sql = "SELECT * FROM customertable WHERE state='$state' "; Quote Link to comment https://forums.phpfreaks.com/topic/196047-phpmyadmin-mysql-database-query/#findComment-1029784 Share on other sites More sharing options...
briankopy Posted March 21, 2010 Author Share Posted March 21, 2010 my bad. Quote Link to comment https://forums.phpfreaks.com/topic/196047-phpmyadmin-mysql-database-query/#findComment-1029792 Share on other sites More sharing options...
MadTechie Posted March 22, 2010 Share Posted March 22, 2010 LOL.. your get used to it.. just takes a little time $sql = "SELECT * FROM customertable WHERE state='$state' "; will find all that have the same state that you posted without the WHERE state='$state' it will show all PS i am happy to help if you want to learn.. Quote Link to comment https://forums.phpfreaks.com/topic/196047-phpmyadmin-mysql-database-query/#findComment-1029800 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.