I am making an PM system for my users.
In the recipient field you type in a full name. When you submit i want to make sure it’s the right user he sends to.
So if he type “Megan Fox”, it’s a full match of a user. But if he type Meg F or Megan, Fox it will return “Did you mean Megan Fox?”
So…
Now I have thought what if there’s more than one with the name “Megan” or the lastname “Fox”. I made a while() for that, and it will return if theres more than 1, “Who did you mean: Matt Fox, Megan Fox?”
Now I havnt found where It goes wrong, I got 3 rows with the lastname “Fox”, it only shows two of them in the “Who did you mean?”
How can I solve this?
And if I only enter a space in the recipient field, it will return all users, in the table. How can i prevent this?
Here’s what my code looks like:
$mot = mysql_real_escape_string($_POST['mottagare']);
list($firstname, $lastname) = array_map('ucfirst', explode(' ', $mot, 2));
$qur = mysql_query("
SELECT id, firstname, lastname,
(firstname = '$firstname' AND lastname = '$lastname') AS full FROM users
WHERE (firstname = '$firstname' AND lastname='$lastname')
OR (firstname LIKE '$firstname%' AND lastname LIKE '$lastname%')
OR firstname LIKE '$firstname%' OR lastname LIKE '$firstname%'
ORDER BY (firstname = '$firstname' AND lastname='$lastname') DESC");
$get = mysql_fetch_array($qur);
if(mysql_num_rows($qur) == 1){
if($get["full"] == 1){
echo $get["id"];
}else{
echo "Did you mean ".$get["firstname"]." ".$get["lastname"]." ?";
}
}elseif(mysql_num_rows($qur) > 1){
while($get = mysql_fetch_array($qur)) {
$name[] = $get["firstname"]." ".$get["lastname"];
}
if(count($name) > 1) {
echo 'Who did you mean?<br>';
} else {
echo 'Did you mean: ';
}
echo implode('<br>', $name);
}
In this line:
you already fetched the first of your names without appending it to the $name array, so in this line:
the first one will not be fetched from the DB again.