Jump to content

[SOLVED] Use a single text form field to make multiple 'OR' selections


jaxdevil

Recommended Posts

Hey guys,

 

What I am trying to do, but can't find any reference to, is have a form submit info to an mySQL query but having one field from the form have data separated by some delimiter (like a ',' or a ' ' or a ';') and have the query replace data in the query with the different listed items. for example... the form would send some information like this:

 

lotnums=12,10,15,375,227,10

 

Then the query would insert the data as follows..

 

SELECT * FROM table123 WHERE lotnum = '12' OR '10' OR '15' OR '375' OR '227' OR '10'

 

 

Any ideas how to do this?

 

Thanks in advance,

SK

Link to comment
Share on other sites

You wouldnt get the '12' quotes in doing that.

You'd need to do:

<?php

$str = "12,10,15,375,227,10";

$split = explode(",",$str);
$num = count($split) -1;
foreach ($split as $key => $value)
{
if	($key ==$num)
	{
	$new .= "'" . $value . "'";
	}
else
	{
	$new .= "'" . $value . "'" . " OR ";
	}
}
echo $new; //contains the string
?>

Link to comment
Share on other sites

You can do this like

<?php
$str = '12,10,15,375,227,10';
$or_str = "'" . str_replace(",","' OR '",$str) . "'";
echo $or_str;
?>

or

<?php
$str = '12,10,15,375,227,10';
$or_str = "'" . implode("' OR '",explode(',',$str)) . "'";
echo $or_str;
?>

 

Ken

Link to comment
Share on other sites

Ummm. An OR test does not work like has been posted and in the answers.

 

Use the mysql IN() test.

 

If the column is a numeric the following will work -

 

WHERE lotnum IN(12,10,15,375,227,10) 

 

If the column is a character type -

 

WHERE lotnum IN('12','10','15','375','227','10') 

Link to comment
Share on other sites

OK, this is the code I tried... but it didn't update JUST those items, it changed EVERY item to equip_type 3. I don't understand it, there are no wild cards in the query. Any ideas?

 

<?php
require($_SERVER['DOCUMENT_ROOT'] . "/config.inc.api.php");
?>
<?php
foreach($_POST as $key => $item)
{
$key = $_POST[$key];
}
?> 
<?php
$str = $lots;
$or_str = "'" . implode("' OR '",explode(',',$str)) . "'";
$q = "UPDATE equipment SET equip_type='3' WHERE tag = ".$or_str."";
$r = mysql_query($q);
mysql_close();
?>

Link to comment
Share on other sites

I just did an echo on the query string, here is exactly what is being queried....

 

UPDATE equipment SET equip_type='3' WHERE tag = '176' OR '358' OR '357' OR '356' OR '355' OR '350' OR '233' OR '232' OR '183' OR '166' OR '246' OR '250' OR '282' OR '335' OR '283' OR '401' OR '109' OR '359' OR '329' OR '344' OR '120' OR '104' OR '506' OR '507' OR '111' OR '109' OR '119' OR '121' OR '126' OR '123' OR '124' OR '127' OR '129' OR '131' OR '102' OR '103' OR '364' OR '365' OR '393' OR '361' OR '360' OR '362' OR '367' OR '363' OR '368' OR '383' OR '382' OR '392' OR '375' OR '373' OR '372' OR '369' OR '370' OR '371' OR '384' OR '385' OR '386' OR '387' OR '391' OR '400' OR '403' OR '394' OR '396' OR '498' OR '452' OR '397' OR '455' OR '469' OR '473' OR '464' OR '470' OR '483' OR '484' OR '474' OR '505' OR '497' OR '435' OR '353' OR '487' OR '480' OR '481' OR '465' OR '493' OR '418' OR '351' OR '137' OR '138' OR '139' OR '140' OR '151' OR '134' OR '135' OR '352' OR '145' OR '144' OR '148' OR '149' OR '146' OR '147' OR '150' OR '153' OR '154' OR '155' OR '158' OR '162' OR '163' OR '417' OR '165' OR '175' OR '179' OR '184' OR '194' OR '197' OR '249' OR '251' OR '250' OR '425' OR '246' OR '245' OR '247' OR '244' OR '239' OR '238' OR '344' OR '328' OR '335' OR '336' OR '260' OR '346' OR '329' OR '260' OR '316' OR '486' OR '199' OR '198' OR '416' OR '421' OR '901'

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.