-1

I am copying a youtube video tutorial for private messaging. The rest of the tutorial works fine, but as soon as I add this function to my site, my entire site goes blank and nothing is shown? No errors or anything, just a white screen? Have I done something wrong here? Here is the function:

    <?php
function fetch_user_ids($usernames){
    foreach ($usernames as &$name){
        $name = mysql_real_escape_string($name);
    }

    $result = mysql_query("SELECT `userid`, `username` FROM `users` WHERE `username` IN ('" . implode("', '", $usernames) . "')");
    $names = array();

    while (($row = mysql_fetch_assoc($result)) !== false){
        $names[$row['username']] = $row['userid'];
    }

    return $names;
}
?>

Here is the script to send the information:

<?php
if (isset($_POST['to'], $_POST['subject'], $_POST['body'])){
    $errors = array();

    if (empty($_POST['to'])){
        $errors[] = 'You must enter atleast one name.';
    }else if (preg_match('#^[a-z, ]+$#i', $_POST['to']) === 0){
        $errors[] = 'The list of names you gave does not look valid.';
    }else{
        $usernames = explode(',', $_POST['to']);

        foreach ($usernames as &$name){
            $name = trim($name);
        }

        $user_ids = fetch_user_ids($usernames);

        if (count($user_ids) !== count($usernames)){
            $errors[] = 'The following users could not be found: ' . implode(', ', array_diff($usernames, array_keys($user_ids)));
        }
    }

    if (empty($_POST['subject'])){
        $errors[] = 'The subject cannot be empty';
    }

    if (empty($_POST['body'])){
        $errors[] = 'You body must have some text!';
    }

    if (empty($errors)){
    //Send message
    }
}

if (isset($errors)){
    if (empty($errors)){
        echo '<div class="msg success">Your message has been sent ! <a href="index.php"> return</a></div>';
    }else{
    foreach ($errors as $error){
        echo '<div class="msg error">', $error, '</div>';
    }
  }
}

?>



<form action="" method="POST">
<div>
<label for="to">To</label>
<input type="text" name="to" id="to" />
</div>
<div>
<label for="subject">Subject</label>
<input type="text" name="subject" id="subject" />
</div>
<div>
<textarea name="body" rows="10" cols="110"></textarea>
</div>
<div>
<input type="submit" value="send" />
</div>
</form>

If I take away the "function" part, I can print the data, so it must be something to do with the function element?

2 Answers2

0

I would suggest changing the !== to != and seeeing if that works, it could be interpreting it has a number and not as a bool

Daniel
  • 1
  • 1
0

Make it simpler. Inside foreach, get rid of &$name and replace it with $name. Also check if your database is returning nothing.

foreach ($usernames as $name){
        $name = mysql_real_escape_string($name);
    }

 $result = mysql_query("SELECT `userid`, `username` FROM `users` WHERE `username` IN ('" . implode("', '", $usernames) . "')");
 // Check if the query itself is failing or not here:
 if(!$result) die("Failed to perform query");
 $names = array();
 // Check if the database is returning any rows or not:
 print_r(mysql_num_rows($result));
 while($row = mysql_fetch_assoc($result)){

         $names[$row['username']] = $row['userid'];

}


    return $names;
Sasanka Panguluri
  • 3,058
  • 4
  • 32
  • 54
  • That works, but when I add the "function" code on top, it blanks the screen again? – user257234 Apr 07 '14 at 15:58
  • If you wrap all this code inside `function A { }`, then you'll need to call the function `A();` where you need to perform those operations. Mark my answer as correct if it helped. – Sasanka Panguluri Apr 07 '14 at 16:05