jaxdevil Posted December 17, 2007 Share Posted December 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
revraz Posted December 17, 2007 Share Posted December 17, 2007 Just do a Replace on the delimiter with what you want to replace it with. Quote Link to comment Share on other sites More sharing options...
papaface Posted December 17, 2007 Share Posted December 17, 2007 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 ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 17, 2007 Share Posted December 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 17, 2007 Share Posted December 17, 2007 fyi on your query you need to alter it a touch <?php $str = '12,10,15,375,227,10'; $or_str = "'" . implode("' OR '",explode(',',$str)) . "'"; $q = "select * from `table123` Where lotnum = {".$or_str."}"; ?> is what is needed I believe Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 17, 2007 Share Posted December 17, 2007 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') Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 17, 2007 Share Posted December 17, 2007 I knew it was something like taht I think the braces are for like <?php $q = "Select * from `table` where Field = "yes" and {Field2 = "no" || Field3 = "yes"}"; ?> Quote Link to comment Share on other sites More sharing options...
jaxdevil Posted December 17, 2007 Author Share Posted December 17, 2007 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(); ?> Quote Link to comment Share on other sites More sharing options...
jaxdevil Posted December 17, 2007 Author Share Posted December 17, 2007 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' Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 17, 2007 Share Posted December 17, 2007 It was already mentioned that OR'ing does not work like that (in any programming language in existence on this planet.) That evaluates to WHERE tag = TRUE See my post above for using the mysql IN() function. Quote Link to comment Share on other sites More sharing options...
jaxdevil Posted December 17, 2007 Author Share Posted December 17, 2007 Thanks man, i missed your post above. that fixed it! AWESOME! SK Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.