Jump to content

[SOLVED] Help searching multiple rows


popcornplya

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/144165-solved-help-searching-multiple-rows/
Share on other sites

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);

 

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.