Jump to content

User search, multiple credentials. - RESOLVED


Mr.x

Recommended Posts

Good Afternoon.

Creating a user search and am having some issues with the sql queries.

Search options are

Username Contains : (Input)
Location : (Input)
Age Minimum (Input) Maximum (Input)

I need it so if they only enter a username it finds that username, or if they only enter a location it finds everyone in that location.

But I also need it so if they enter a location and age, it only retrieves people who are that age and live there. etc.

Thanks in advance,
Sean.
i did not test this, so there might be errors

[code]
<?php
$uname = $_POST[username];
$locate = $_POST[location];
$age = $_POST[age];

if(isset($uname) && !$locate && !$age){
$sql = "SELECT * FROM `users` WHERE username LIKE '$uname'";
$res = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_assoc($res)){
echo "<a href=page.php?username=$row[username]>$row[username]</a><br><br>";
}
mysql_free_result($res);
}else

if(isset($uname) && isset($locate) && !$age){
$sql = "SELECT * FROM `users` WHERE username LIKE '$uname' AND location LIKE '$locate'";
$res = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_assoc($res)){
echo "<a href=page.php?username=$row[username]>$row[username] in $row[location]</a><br><br>";
}
mysql_free_result($res);
}else

if(isset($uname) && isset($locate) && isset($age)){
$sql = "SELECT * FROM `users` WHERE username LIKE '$uname' AND location LIKE '$locate' AND age =$age";
$res = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_assoc($res)){
echo "Age: $row[age] <a href=page.php?username=$row[username]>$row[username] in $row[location]</a>";
}
mysql_free_result($res);
}else

if(isset($uname) && isset($age) && !$locate){
$sql = "SELECT * FROM `users` WHERE username LIKE '$uname' AND age =$age";
$res = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_assoc($res)){
echo "Age: $row[age] <a href=page.php?username=$row[username]>$row[username]</a>";
}
mysql_free_result($res);
}else
if(isset($locate) && isset($age) && !$uname){
$sql = "SELECT * FROM `users` WHERE location LIKE '$locate' AND age =$age";
$res = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_assoc($res)){
echo "Age: $row[age] <a href=page.php?username=$row[username]>$row[username] in $row[location]</a><br><br>";
}
mysql_free_result($res);
}else
if(isset($locate) && !$age && !$uname){
$sql = "SELECT * FROM `users` WHERE location LIKE '$locate'";
$res = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_assoc($res)){
echo "<a href=page.php?username=$row[username]>$row[username] in $row[locate]</a>"
}
mysql_free_result($res);
}else

if(isset($age) && !$locate && !$uname){
$sql = "SELECT * FROM `users` WHERE age =$age";
$res = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_assoc($res)){
echo "Age: $row[age] <a href=page.php?username=$row[username]>$row[username]</a><br><br>";
}
mysql_free_result($res);
}else

if(!$locate && !$age && !$uname){
echo "<form name=find method=post action=searchuser.php>\n";
echo "username: <input type=text name=username>\n";
echo "location: <input type=text name=location>\n";
echo "age: <input type=text name=age>\n";
echo "<input type=submit value=search>\n";
echo "</form>";
};
?>
[/code]

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.