popcornplya Posted February 7, 2009 Share Posted February 7, 2009 My code errors when you fill in 2 or more fields. <form action="index.php" method="get"> <table> <tr> <td>User</td><td>Name</td><td>Number</td><td>Address</td><td>Email</td><td>Website</td> </tr> <tr> <td><input type="text" name="user" /></td><td><input type="text" name="name" /></td><td><input type="text" name="number" /></td><td><input type="text" name="address" /></td><td><input type="text" name="email" /></td><td><input type="text" name="website" /></td><td><input type="submit" value="Search" name="search" /></td> </tr> </table> </form> <?php if($_GET['search']) { // slash() is custom function $name = slash($_GET['name']); $number = slash($_GET['number']); $address = slash($_GET['address']); $email = slash($_GET['email']); $website = slash($_GET['website']); $user = slash($_GET['user']); if($name || $number || $address || $email || $website || $user) { $query = "SELECT * FROM `card` WHERE "; if($user) { $query .= "`user` LIKE '%$user%'"; } if($name) { $query .= " `name` LIKE '%$name%'"; } if($number) { $query .= " `number` LIKE '%$number%'"; } if($address) { $query .= " `number` LIKE '%$address%'"; } if($email) { $query .= " `number` LIKE '%$email%'"; } if($website) { $query .= " `number` LIKE '%$website%'"; } $sql = mysql_query($query) or die(mysql_error()); if(mysql_num_rows($sql) >= 1) { while($fetch = mysql_fetch_assoc($sql)) { $user = $fetch['user']; echo $user; } } else { echo "no results"; } } else { echo "enter something"; } } ?> Any help? Quote Link to comment https://forums.phpfreaks.com/topic/144165-solved-help-searching-multiple-rows/ Share on other sites More sharing options...
.josh Posted February 7, 2009 Share Posted February 7, 2009 ... where column like '...' AND column like '....' ... where column like '...' OR column like '....' Quote Link to comment https://forums.phpfreaks.com/topic/144165-solved-help-searching-multiple-rows/#findComment-756502 Share on other sites More sharing options...
popcornplya Posted February 7, 2009 Author Share Posted February 7, 2009 ... where column like '...' AND column like '....' ... where column like '...' OR column like '....' but what if there was nothing before it? Quote Link to comment https://forums.phpfreaks.com/topic/144165-solved-help-searching-multiple-rows/#findComment-756506 Share on other sites More sharing options...
gevans Posted February 7, 2009 Share Posted February 7, 2009 you can trick it with a 1=1 WHERE 1=1 ...then do any comparissons you want Quote Link to comment https://forums.phpfreaks.com/topic/144165-solved-help-searching-multiple-rows/#findComment-756507 Share on other sites More sharing options...
.josh Posted February 7, 2009 Share Posted February 7, 2009 You can just add AND/OR to the beginning or end of each string and chop off the last 3 or 2 chars after your conditions, using substr for instance. Or instead of doing $query .= ... each condition, do like $subquery[] = .... and then after the last condition, you could do $query .= implode('AND', $subquery); Quote Link to comment https://forums.phpfreaks.com/topic/144165-solved-help-searching-multiple-rows/#findComment-756508 Share on other sites More sharing options...
popcornplya Posted February 7, 2009 Author Share Posted February 7, 2009 You can just add AND/OR to the beginning or end of each string and chop off the last 3 or 2 chars after your conditions, using substr for instance. Or instead of doing $query .= ... each condition, do like $subquery[] = .... and then after the last condition, you could do $query .= implode('AND', $subquery); OH! thanks! Quote Link to comment https://forums.phpfreaks.com/topic/144165-solved-help-searching-multiple-rows/#findComment-756511 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.