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

 

Link to comment
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);

OH! thanks!

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.