PHP select, where all results equal LIKE $_POST

Hi Guys/ Girls,

PHP is not my strong suit but am plodding along… am really stuck on this one and am not sure it is even possible.

I have a form that displays customer records (first name, last name, address, tel numbers, email etc). I have a basic filter set up using POST data. I have one input field for each table row (i.e first name, last name). So if i enter Andy in the first name field and submit the data is filtered and only fist names that are like andy are displayed.

I want to change this so I only have one filter box that searches the entire record. So if I typed andy it would show results where andy apears in the first name, last name, address etc.

Is this possible?

Here is the code I currently have, any hel would be great!

[php]$cust_query = "SELECT * FROM customers c LEFT JOIN address_book a ON a.address_book_id = c.customers_default_address_id

WHERE c.customers_firstname LIKE ‘%$_POST[firstname]%’
AND c.customers_lastname LIKE ‘%$_POST[lastname]%’
AND a.entry_postcode LIKE ‘%$_POST[postcode]%’
AND c.customers_email_address LIKE ‘%$_POST[email]%’

LIMIT 500
";

$cust = $db->Execute($cust_query);

$custArray = array();
while (!$cust->EOF) {

$custArray[] = array(‘customers_firstname’=>$cust->fields[‘customers_firstname’],
‘customers_lastname’=>$cust->fields[‘customers_lastname’],
‘entry_company’=>$cust->fields[‘entry_company’],
‘entry_street_address’=>$cust->fields[‘entry_street_address’],
‘entry_suburb’=>$cust->fields[‘entry_suburb’],
‘entry_city’=>$cust->fields[‘entry_city’],
‘entry_state’=>$cust->fields[‘entry_state’],
‘entry_postcode’=>$cust->fields[‘entry_postcode’],
‘entry_firstname’=>$cust->fields[‘entry_firstname’],
‘entry_lastname’=>$cust->fields[‘entry_lastname’],
‘customers_telephone’=>$cust->fields[‘customers_telephone’],
‘customers_fax’=>$cust->fields[‘customers_fax’],
‘customers_mobile’=>$cust->fields[‘customers_mobile’],
‘customers_other’=>$cust->fields[‘customers_other’],
‘customers_email_address’=>$cust->fields[‘customers_email_address’],
‘customers_id’=>$cust->fields[‘customers_id’],

);

$cust->MoveNext();
}

<?php foreach($custArray as $cust) { ?> <?php } ?>
Account Customer Details
<?php echo '' . $cust['customers_id'] . ''; ?> <?php echo $cust['customers_firstname']; ?> <?php echo $cust['customers_lastname']; ?> <?php echo $cust['entry_company']; ?> - <?php echo $cust['entry_street_address']; ?> <?php echo $cust['entry_suburb']; ?> <?php echo $cust['entry_city']; ?> <?php echo $cust['entry_state']; ?> <?php echo $cust['entry_postcode']; ?>
[/php]

Yes, it can be done. If you are searching every column for the same exact thing, it suggests your database is not properly designed.

Live example: http://www.sqlfiddle.com/#!2/082d9b/4

[code]CREATE TABLE articles (
id int(11) NOT NULL AUTO_INCREMENT,
title varchar(200) DEFAULT NULL,
body text,
PRIMARY KEY (id),
FULLTEXT KEY title (title,body)
) ENGINE=MyISAM;

INSERT INTO articles VALUES (1,‘MySQL Tutorial’,‘DBMS stands for DataBase …’),(2,‘How To Use MySQL Well’,‘After you went through a …’),(3,‘Optimizing MySQL’,‘In this tutorial we will show …’),(4,‘1001 MySQL Tricks’,‘1. Never run mysqld as root. 2. …’),(5,‘MySQL vs. YourSQL’,‘In the following database comparison …’),(6,‘MySQL Security’,‘When configured properly, MySQL …’);[/code]

select * from articles WHERE MATCH(title,body) AGAINST ('database');
Sponsor our Newsletter | Privacy Policy | Terms of Service