473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to build query string?

From a form I have a list of fields, e.g:

name
address
phone
memo

I know how I do it manually for search query but how do I do it
automatically.

select * from <table_name> where
name = %$_POST['name']% and/or
address LIKE %$_POST['address']% and/or
phone LIKE %$_POST['phone']% and/or
memo LIKE %$_POST['memo']%

but my trouble if one of them is blank. Thanks

Jul 17 '05 #1
3 8302
Artco News wrote:
From a form I have a list of fields, e.g:

name
address
phone
memo

I know how I do it manually for search query but how do I do it
automatically.

select * from <table_name> where
name = %$_POST['name']% and/or
address LIKE %$_POST['address']% and/or
phone LIKE %$_POST['phone']% and/or
memo LIKE %$_POST['memo']%

but my trouble if one of them is blank. Thanks


A simple way to do it :

function getPostVar($varName, $default='')
{
if (isset($_POST[$varName])) {
$var = $_POST[$varName];
}
else {
$var = $default;
}
return $var;
}
function buildWhereClause($name, $adressAndOr,
$address, $phoneAndOr,
$phone, $memoAndOr, $memo)
{
$with_name = strlen($name) > 0;
$with_address = strlen($address) > 0;
$with_phone = strlen($phone) > 0;
$with_memo = strlen($memo) > 0;

$clause = '';

if ($with_name || $with_address || $with_phone || $with_memo) {
$clause = " where ";
if ($with_name) {
$clause .= " name = '$name' ";
if ($with_address) {
$clause .= $adressAndOr;
}
}
if ($with_address) {
$clause .= " address like %'$address' ";
if ($with_phone) {
$clause .= $phoneAndOr;
}
}
/* etc ... */
}
return $clause;
}

/* possible use... */
$where = buildWhereClause(getPostVar('name'), 'and',
getPostVar('address'), 'or',
getPostVar('phone'), 'and',
getPostVar('memo'));

$query = "select * from table_name " . $where;

Note that you should add some code to the getPostVar() function to make
sure no one is trying to attack your system.

HTH
Bruno

Jul 17 '05 #2
On Mon, 03 Nov 2003 22:01:46 GMT, "Artco News" <ar*******@verizon.net> wrote:
From a form I have a list of fields, e.g:

name
address
phone
memo

I know how I do it manually for search query but how do I do it
automatically.

select * from <table_name> where
name = %$_POST['name']% and/or
address LIKE %$_POST['address']% and/or
phone LIKE %$_POST['phone']% and/or
memo LIKE %$_POST['memo']%

but my trouble if one of them is blank. Thanks


What trouble are you having? You end up with a redundant predicate (like '%%',
which always matches - well, except for NULLs anyway), but it would still work.

How are you determining whether you want AND or OR anyway?

Do you want something like (assuming MySQL where you have to stuff values in
SQL, rather than use placeholders):

<?php
$queryable_fields = array('name', 'address', 'phone', 'memo');

$sql = 'select * from tab where 1=1 ';

foreach ($queryable_fields as $field)
if (isset($_POST[$field]))
$sql .= sprintf("and %s LIKE '%s%' ",
$field,
'%'.addslashes($_POST[$field]).'%');

echo $sql;
?>

(The 1=1 bit being there so you don't have to bother trimming off an 'and' at
one end)

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #3
Hi Artco!
On Mon, 03 Nov 2003 22:01:46 GMT, "Artco News" <ar*******@verizon.net>
wrote:
From a form I have a list of fields, e.g:

name
address
phone
memo

I know how I do it manually for search query but how do I do it
automatically.

select * from <table_name> where
name = %$_POST['name']% and/or
address LIKE %$_POST['address']% and/or
phone LIKE %$_POST['phone']% and/or
memo LIKE %$_POST['memo']%

but my trouble if one of them is blank. Thanks


Have a look at dal.php from the CVS from the project in the signature.

HTH, Jochen
--
Jochen Daum - CANS Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: grunar | last post by:
After some thought on what I need in a Python ORM (multiple primary keys, complex joins, case statements etc.), and after having built these libraries for other un-named languages, I decided to...
1
by: Mark | last post by:
My Category and Product tables look like: TblCategory CategoryID Category TblProduct ProductID CategoryID Product
0
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list...
22
by: argniw | last post by:
I have a table that looks like this: Forest Residents Black Josh Black Joellen Black Mary Jane Brown Gertrude Brown Josh Brown Mary Jane
2
by: tsteinke | last post by:
Is there a function in C# to Build a Query String for an HttpRequest? Like from a NameValue Collection or something?
2
by: Howard | last post by:
How do I do this with parameterized query? without parameterized query: string search = "hello world search"; search = search.Replace(" ", " AND "); SELECT * FROM TABLE1 WHERE TEXT LIKE '%' +...
6
by: dotnetnoob | last post by:
i would like to know how i can build xpath expression dynamiclly. let's say i have a following xml file: <EventEnrollment InstanceNumber = "675"> <EventSource> <ObjectReference...
1
by: durumdara | last post by:
Hi! I have a pager component. This component is makes database slices (record sets) and it is use query string's PageIndex element to identify the actual page. This component is makes pager...
11
by: =?Utf-8?B?U3RlZmFuIFdpbGhlbG0=?= | last post by:
Hello, played a little with orcas and went into a problem by building dynamic queries. I want to build a dynamic where clause but only with parameters that are not empty =(TextBox.Text != "")....
0
petepell
by: petepell | last post by:
Hello all, I am developing an application in VB 2008 that works with a SQL2005 DB to store and manipulate employee data. In one section of the app I want to be able to show a treeview of the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.