Forum Moderators: coopster

Message Too Old, No Replies

search multiple records and display results

         

brentc73

2:00 am on Mar 18, 2008 (gmt 0)

10+ Year Member



I have a database with 100 records and basically I want someone to be able to search these records using the same field and then have php display the results.

Ex:
field 1 = first name
field 2 = first name
field 3 = first name
field 4 = first name
upto
field 20 = first name

they can fill out the form (either all the fields are just 1) and then have the results displayed.

Thanks in advance.

coopster

9:44 pm on Mar 18, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, brentc73.

You could loop through the posted form variables and if they are set and have a value that you might expect to see (numbers for a numeric field, etc.) you could append that name/value pair to your SQL query.

brentc73

12:58 am on Mar 19, 2008 (gmt 0)

10+ Year Member



Thank you, very much appreciated!
I am pretty much a newbie, could you possibly give me an example..?

coopster

1:45 am on Mar 19, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



OK, let's say you developed a form and you set the form method to POST. I'll use general terms here, but it would go something like this after you have scrubbed the incoming data ...
// this is where you make sure your data is clean 
// NOTE: mysql_real_escape_string [php.net] is bare minimum!
$fname = mysql_real_escape_string($_POST['fname']);
$mname = mysql_real_escape_string($_POST['mname']);
$lname = mysql_real_escape_string($_POST['lname']);
// ... rest of your form data validation, then ...
$where = 'WHERE 1=1'; // initialize
if ($fname) {
$where .= " AND mytable.fname = '$fname'";
}
if ($mname) {
$where .= " AND mytable.mname = '$mname'";
}
if ($lname) {
$where .= " AND mytable.lname = '$lname'";
}
$sql = "SELECT * FROM mytable $where";

On a side note, this is not the most efficient way to query your table. Searching multiple fields for a value, even a first name, is going to require extra work on a larger table to get indexing as you would like.

brentc73

6:03 pm on Mar 19, 2008 (gmt 0)

10+ Year Member



Thank you, Not sure I understand.

I currently have have a form with the following:

---form----
<form id="form1" name="form1" method="post" action="search.php">
<input type="text" name="item_number" id="textfield" />
<label>
<input type="submit" name="get" id="get" value="Submit" />
</label>
</form>
-----------------

and search.php with the following

$item_number = $_POST['item_number'];

$query = "SELECT item_number, supplier, item_desc, cbm, case_pk, price FROM $table where item_number = '$item_number'";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_NUM))
{
echo "Item Number :{$row[0]} <br>" .
"Supplier : {$row[1]} <br>" .
"Item Description : {$row[2]} <br>" .
"CBM : {$row[3]} <br>" .
"Case Pack : {$row[4]} <br>" .
"Price : {$row[5]} <br><br>";
}
?>

Problem is is that I want the form to be like this

---form----
<form id="form1" name="form1" method="post" action="search.php">
<input type="text" name="item_number" id="textfield" />
<input type="text" name="item_number" id="textfield" />
<input type="text" name="item_number" id="textfield" />
<input type="text" name="item_number" id="textfield" />
<label>
<input type="submit" name="get" id="get" value="Submit" />
</label>
</form>

and return the results of all the item numbers that are searched.

Does this make sense?