form (?) problem

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
i have a form with 5 fields. and my problem is that if user put 2 or more
same variables my script finds in db only 1 and i need to search all
variables in db even if they r same.

$query ="SELECT matom,nazwa FROM chem WHERE symb LIKE '$A' OR symb LIKE '$B'
OR symb LIKE

'$C' OR symb LIKE '$D' OR symb LIKE '$E'";
$result = mysql_query ($query) or die ("Zapytanie zakonczone
for ($i=0;$i<$wiersze;$i++) {
    $wiersz = mysql_fetch_array($result);
    echo "Masa molowa <b>$b"."u</b> wynosi $a<br>";

    echo "<br>".$M;


Re: form (?) problem


If the question is how can you build an SQL query that tests a variable
amount of LIKE conditions .. then I've used this strategy before. Possibly
not the best solution and/or could do with some tidying by a better coder.

Change the following:

Quoted text here. Click to load it

Into this:

$A="OR symb LIKE . "$_POST['A'];
$u="OR symb LIKE . "$_POST['u'];
$B="OR symb LIKE . "$_POST['B'];

and so on for the remaining variables. This is building snippets of SQL.

Because you want the _first_ SQL snippet to _not_ begin with 'OR' (but all
preceding snippets to) .. I'd write a switch, and list your variables in the
exact order that you did above. This switch code will take out the three
character 'OR ' from the beginning of first variable that has been assigned

switch (TRUE) {
case $A:
   $A = substr($A, 3);
case $u:
   $u= substr($u, 3);
case $B:
   $B= substr($B, 3);

Add another 'case' for every remaining variable, following the same format.

Then your SQL query will look like this

$query ="SELECT matom,nazwa FROM chem WHERE $A $u $B";

Add the remainging variables in the same order as you have listed them
before, one after the other with spaces in between. Where a value for a
variable doesn't exist, it will simply be nothing, i.e., not part of the
query. The first variable will have its 'OR ' removed.

Um .. I hope this works anyway :>

Ka Kite

Quoted text here. Click to load it
Quoted text here. Click to load it
Quoted text here. Click to load it

Re: form (?) problem

Doh! Noticed a bug already.

Quoted text here. Click to load it

Sorry, you'll have to change that suggestion, if the 'switch' is to work as

Instead, change to:

if ($_POST['A']) { $A="OR symb LIKE . "$_POST['A']; }
if ($_POST['u']) { $u="OR symb LIKE . "$_POST['u']; }
if ($_POST[B]) { $B="OR symb LIKE . "$_POST['B']; }

And so on for the remaining variables

Site Timeline