Jump to content

PHP code needed to get/post form fields and request mysql query results.


Recommended Posts

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]

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

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";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.