Hi there I am trying to make a search query output results to the web page that the user is viewing. I have 2 fields in the html, the search should query the db using either field if only one was entered or both if the two were entered…
Then I want to print the results to the screen, I’ve been told to use a foreach statement but am not sure how to use it so I have copied the php below:
[code]#!/usr/local/bin/php
<? session_start(); session_register('lname'); session_register('fname'); session_register('addr'); session_register('password'); session_register('in'); $in = "0"; //Check to see if 1 or both of fields were filled in if(!$_POST[lname] && !$_POST[addr]) { echo 'Please fill in the required fields before clicking the Find Customer button
'; echo 'TRY AGAIN!
'; exit; } if(!$_POST[lname]) { //setup names of database and table to use $db_name = "baz"; $table_name = "Customers"; //connect to server and select database $connection = @mysql_connect("mysql.internal", "baz", "rREfp2t4") or die(mysql_error()); //build and issue the query $sql = "SELECT * FROM $db_name.$table_name WHERE addr2 = '$_POST[addr]'"; $result = @mysql_query($sql, $connection) or die("result error: " . mysql_error()); $name = mysql_fetch_row($result); $fname = $name[0]; $lname = $name[1]; $addr1 = $name[2]; $addr2 = $name[3]; $addr3 = $name[4]; $addr4 = "Dublin" . $name[5]; $phNum = $name[6]; $mobNo = $name[7]; $email = $name[8]; exit; } else if(!$_POST[addr]) { //setup names of database and table to use $db_name = "baz"; $table_name = "Customers"; //connect to server and select database $connection = @mysql_connect("mysql.internal", "baz", "rREfp2t4") or die(mysql_error()); //build and issue the query $sql = "SELECT * FROM $db_name.$table_name WHERE l_name = '$_POST[lname]'"; $result = @mysql_query($sql, $connection) or die("result error: " . mysql_error()); $name = mysql_fetch_row($result); $fname = $name[0]; $lname = $name[1]; $addr1 = $name[2]; $addr2 = $name[3]; $addr3 = $name[4]; $addr4 = "Dublin" . $name[5]; $phNum = $name[6]; $mobNo = $name[7]; $email = $name[8]; exit; } else { //setup names of database and table to use $db_name = "baz"; $table_name = "Customers"; //connect to server and select database $connection = @mysql_connect("mysql.internal", "baz", "rREfp2t4") or die(mysql_error()); //build and issue the query $sql = "SELECT * FROM $db_name.$table_name WHERE addr2 = '$_POST[addr]' AND l_name = '$_POST[lname]'"; $result = @mysql_query($sql, $connection) or die("result error: " . mysql_error()); $name = mysql_fetch_row($result); $fname = $name[0]; $lname = $name[1]; $addr1 = $name[2]; $addr2 = $name[3]; $addr3 = $name[4]; $addr4 = "Dublin" . $name[5]; $phNum = $name[6]; $mobNo = $name[7]; $email = $name[8]; exit; } //Add fields to the database!! //Display the new updated results!!! ?> ABC Heating | 4th Year Project Add Staff Member | Delete Staff Member | Update Jobs List | Search For Customer Details | Change Customer Details
<font size=2 color="Black"
The Customers found matching your criteria are as follows:
First Name: <? echo "$fname"; ?>
Last Name: <? echo "$lname"; ?>
Address:
<? echo "$addr1"; ?>
<? echo "$addr2"; ?>
<? echo "$addr3"; ?>
<? echo "$addr4"; ?>
Phone No: <? echo "$phNum"; ?>
Mobile No: <? echo "$mobNo"; ?>
E-mail Address: <? echo "$email"; ?>
[/code]