cbyrns1125 Posted January 9, 2010 Share Posted January 9, 2010 I'm new to PHP I've created an html form to allow the user to request a basic telephone directory listing. They can select their 'listtype' by 'all' or by 'state'. If they choose 'state' then they must select a state from the dropdown menu - the state values are normal abbreviations. IE: 'AL' In my php page I need to gather the users selection and retrieve the specific data from a table named 'contacts' then display it in a table sorted by last name. Attached is what I've gotten so far. I had to attach the html in as a txt file since it wasn't allowed. Thanks so much! [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/187888-php-code-needed-to-getpost-form-fields-and-request-mysql-query-results/ Share on other sites More sharing options...
Catfish Posted January 9, 2010 Share Posted January 9, 2010 There was a few things wrong with your php code from what I understand you want to do, so I just recoded it for you to have a look. take note of the comments: <?php // use require() to include a specified file require 'db_connection.php'; // set the sql query if ($_POST['type'] == 'state') { $state = mysql_real_escape_string($_POST['state']); $sql = "select * from contacts where state = '$state'"; } else $sql = "select * from contacts"; // fetch a result if ($QueryResult = @mysql_query($connection, $SQLstring)) { // output table headings echo "<table border='1'><h2>Directory Listing</h2>\n"; echo "<tr><th>Last name</th>"; echo "<th>First name</th>"; echo "<th>Area code</th>"; echo "<th>Phone number</th></tr>\n"; // loop through all results and output them in the table while ($Row = mysql_fetch_assoc($QueryResult) { echo "<tr>"; // output each field value in the record foreach ($Row as $fieldName => $value) echo "<td>$value</td>"; echo "</tr>\n"; } echo "</table>\n"; } else die(mysql_error($connection)); // die on result fetch failure mysql_close($connection); ?> I changes the functions from mysqli_ to mysql_ as I dont know what mysqli is, it looks like its for working with objects and your code doesn't show any need for that. If you know why you're using mysqli then use it otherwise mysql_ functions will do fine. Your code's flow was off, there's no way you would have gotten results to be output. You were setting the SQL query after you'd already sent a different query. The flow of the new code is: 1. set the sql query 2. fetch a result -> else fail and die() 3. output table headings 4. loop throug hresults and output them into the table 5. close table and html Quote Link to comment https://forums.phpfreaks.com/topic/187888-php-code-needed-to-getpost-form-fields-and-request-mysql-query-results/#findComment-992028 Share on other sites More sharing options...
mikesta707 Posted January 9, 2010 Share Posted January 9, 2010 mysqli stands for mysql improved by the way. Quote Link to comment https://forums.phpfreaks.com/topic/187888-php-code-needed-to-getpost-form-fields-and-request-mysql-query-results/#findComment-992031 Share on other sites More sharing options...
Catfish Posted January 10, 2010 Share Posted January 10, 2010 should we use mysqli instead of mysql then? Quote Link to comment https://forums.phpfreaks.com/topic/187888-php-code-needed-to-getpost-form-fields-and-request-mysql-query-results/#findComment-992038 Share on other sites More sharing options...
cbyrns1125 Posted January 10, 2010 Author Share Posted January 10, 2010 Thanks for your reply. I'm getting a: Undefined index at line 23. Which is this area... // set the sql query if ($_POST['type'] == 'state') { $state = mysql_real_escape_string($_POST['state']); $sql = "select * from contacts where state = '$state'"; } else $sql = "select * from contacts"; Quote Link to comment https://forums.phpfreaks.com/topic/187888-php-code-needed-to-getpost-form-fields-and-request-mysql-query-results/#findComment-992046 Share on other sites More sharing options...
Catfish Posted January 10, 2010 Share Posted January 10, 2010 which is line 23? Check to see if all the indexes are set. One of them isnt. Quote Link to comment https://forums.phpfreaks.com/topic/187888-php-code-needed-to-getpost-form-fields-and-request-mysql-query-results/#findComment-992258 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.