MySQL Forums
Forum List  »  Newbie

Search table using 1, 2 or 3 inputs
Posted by: Nathan Czachur
Date: January 28, 2013 02:59PM

Hey guys, first post here.

I have a table of users (id, firstname, lastname...) and a form to insert new users.

My problem is searching this table. If I want all users with surname "Jones" and privilege "Teacher", or only one input, how do I go about this?

I can only get it to work if all variables have been entered by selecting that row from the table.

Thanks

Nathan

$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$privilege = $_POST['privilege'];

$sql ="SELECT * FROM `users` WHERE `userFirstname`='$firstname' AND `userSurname `='$surname ' AND `userPrivilege `='$privilege '";

$result= mysql_query($sql) or die(mysql_error());

if(mysql_num_rows($result) > 0){
while ($row = mysql_fetch_assoc($result)){
echo "
<tr>
<td>".$row['userFirstname']."</td>
<td>".$row['userSurname']."</td>
<td>".$row['userPrivileges']."</td>
</tr>";
}
}

?>

Options: ReplyQuote


Subject
Written By
Posted
Search table using 1, 2 or 3 inputs
January 28, 2013 02:59PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.