I want to display just the state with last, first, areacode and phone number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mad Dog

    #1

    I want to display just the state with last, first, areacode and phone number

    No code errors jsut I displaqy all the states when the just state submit button is selected?
    Code:
     <?php
    if(isset($_POST['state'])){$state = $_POST['state'];}
    if(isset($_POST['allrecords'])){$allrecords = $_POST['allrecords'];}
    if(isset($_POST['sort'])){$sort = $_POST['sort'];}
    if(isset($_POST['ascdsc'])){$ascdsc = $_POST['ascdsc'];}
     
    $Connection = mysqli_connect("localhost", "jmcgoff", "jmcgoff", "dennis");
     
    if(isset($_POST["state"])) {	
    	$query = "SELECT lastName, firstName, areaCode, phoneNumber FROM `contact` WHERE state = '$state'";
    
              if(isset($sort)) {
    			   $query .=" ORDER BY $sort $ascdsc";
    			}
    	$result1 = mysqli_query($Connection, $query);
    }
     while ($data = mysqli_fetch_array($result1)) { 
     echo "<table>";
     echo "<tr><td>$data[state]</td></tr>";
    echo "</table>";
    	}
    if (isset($_POST["state"])) {
    	$query = "SELECT * FROM `contact`";
     
    		if(isset($sort)) {
    			$query .=" ORDER BY $sort $ascdsc";
    			}
    	$result = mysqli_query($Connection, $query);
    }
     
    while ($data = mysqli_fetch_array($result)) { 
    echo "<table border='1'>
    	<tr>
    	<th>ID</th>
    	<th>Lastname</th>
    	<th>Firstname</th>
    	<th>Address</th>
    	<th>City</th>
    	<th>State</th>
    	<th>Zip Code</th>
    	<th>Area Code</th>
    	<th>Phone Number</th>
    	</tr>";
    	
    	echo "<tr>";
    	echo "<td>" . $data['contactID'] . "</td>";
    	echo "<td>" . $data['lastName'] . "</td>";
    	echo "<td>" . $data['firstName'] . "</td>";
    	echo "<td>" . $data['address'] . "</td>";
    	echo "<td>" . $data['city'] . "</td>";
    	echo "<td>" . $data['state'] . "</td>";
    	echo "<td>" . $data['zipCode'] . "</td>";
    	echo "<td>" . $data['areaCode'] . "</td>";
    	echo "<td>" . $data['phoneNumber'] . "</td>";
    	echo "</table>";
    }
     echo '<br /><a href="retrieve.html">Retrieve Data</a>';
     echo '<br /><a href="contact.html">Contact Form</a>';
    mysqli_close($Connection);
     
    ?>
  • JKing
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    #2
    You have two blocks of code that query the database. One with a WHERE clause and one with the wildcard * to select all records. You execute and print both result sets.

    I'm going to guess here that you want the if block on line 22 to be checking $_POST['allrecords'] not state again.

    Also you should use mysql_real_esca pe_string() on variables you pass to the database to avoid sql injection attacks.

    Comment

    Working...