SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Contact Search Form

    I'm new to PHP. I've been practicing by making a contact website. I'm using a MySql DB. I've created the search form but when I search by last name I don't get any results just the top column names. What am I doing wrong? Here is the code:

    Search Form
    Code:
     
    <form method="post" action="search_result.php">
    <table width="300">
    <tr> 
    	  <td width="100" class="formtext">ID:</td>
    	  <td> 
    		<input type="text" name="id" size="6" maxlength="6" class="formtext">
    	  </td>
    	</tr>
      <tr> 
    	  <td width="100" class="formtext">First Name:</td>
    	  <td> 
    		<input type="text" name="first" size="15" maxlength="15" class="formtext">
    	  </td>
    	</tr>
     <tr>
    	  <td width="100" class="formtext">Last Name:</td>
    	  <td> 
    		<input type="text" name="last" size="15" maxlength="15" class="formtext">
    	  </td>
    	</tr>
     <tr> 
    	  <td colspan="2" valign="top"> 
    		<div align="center"> 
    		  <input type="submit" name="submit" value="Search Contacts" class="formtext">
    		</div>
    	  </td>
    	</tr>
    </table>
    </form>
    search_result.php
    Code:
     
    <?php 
    if ((!$_POST[last])) {
    header( "Location: http://192.168.123.146/contacts/menu.htm");
    exit;
    }
     
    $db_name = "contacts";
    $table_name = "contacts";
     
    $connection = @mysql_connect("localhost", "username", "password") or die(mysql_error());
    
    $db = @mysql_select_db($db_name, $connection) or die(mysql_error());
     
    $sql = "select id, first, last from $table_name where last = '$last'";
    $result = @mysql_query($sql, $connection) or die(mysql_error());
     
    while ($row = mysql_fetch_array($result)) {
    $id = $row['id'];
    $first = $row['first'];
    $last = $row['last'];
    $phone = $row['phone'];
    $mobile = $row['mobile'];
    $fax = $row['fax'];
    $email = $row['email'];
    $web = $row['web'];
    }
    $display_block .= "
       <table border=1 width=400>
       <tr>
       <td class=formtext>ID</td>
       <td class=formtext>First Name</td>
       <td class=formtext>Last Name</td>
       </tr>
       <tr><td colspan=3 width=400></td></tr>
       <tr>
       <td class=formtext>$id</td>
       <td class=formtext>$first</td>
       <td class=formtext>$last</td>
       </tr>
       </table>";
    ?>

  2. #2
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $display_block = '';
    while ($row = mysql_fetch_array($result)) {
    $id = $row['id'];
    $first = $row['first'];
    $last = $row['last'];
    $phone = $row['phone'];
    $mobile = $row['mobile'];
    $fax = $row['fax'];
    $email = $row['email'];
    $web = $row['web'];
    
    $display_block .= "
       <table border=1 width=400>
       <tr>
       <td class=formtext>ID</td>
       <td class=formtext>First Name</td>
       <td class=formtext>Last Name</td>
       </tr>
       <tr><td colspan=3 width=400></td></tr>
       <tr>
       <td class=formtext>$id</td>
       <td class=formtext>$first</td>
       <td class=formtext>$last</td>
       </tr>
       </table>";
    }
    echo $display_block;
    Mike
    It's not who I am underneath, but what I do that defines me.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •