Wendy O.
Wendy O.

Reputation: 191

My call to a function is apparently incorrect, I know it's not, but it's not clear why

I'm trying a tutorial about making a private inbox feature, everything went well apart from the fact I get a 500 server error every time I try to press send. I have checked out the logs for what could be causing this error and here's what I received: PHP Fatal error: Call to undefined function fetch_users_id() in /apps/bla/web/inboxPage.php on line 17, referer: http://hinat.local/inboxPage.php

I have checked the function to see if anything is out of place, but cannot spot anything that could be throwing it off.

Would appreciate another pair of eyes to help me see what I have done wrong here.

Thanks in advance!

inboxPage.php:

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

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

//Will remove and trailing spaces before and after name
     foreach ($user_names as &$name){
       $name = trim($name);
     }

     $user_id = fetch_users_id($user_names);

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

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

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

   if(empty($errors)){

   }
 }

 if(isset($errors)){
   //Form has been submitted but errors have occured
   if(empty($errors)){
     echo '<div class="msg success"> Your message has been sent! <a href="inboxPage.php">Return to your Inbox</a></div>';
  //Form has been submittied and errors have occured
   } else {
     foreach ($errors as $errors) {
       echo '<div class="msg error">', $errors, '</div>';
     }
   }
 }
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="site.css" >
<link href="https://fonts.googleapis.com/css?family=Lato:100,300,400" rel="stylesheet">
</head>

<body>

<!-- Header -->

<header class="primary-header container group">
  <h1 class="logo">
      <!-- <img src="../home/wendy/Pictures/Logo.png" alt="Website Logo"><br> -->
    <a href="index.php">  </a>
    </h1>

    <h3 class="tagline"> Cardiff, Wales </h3>

    <nav class="nav primary-nav">
      <ul>
        <li><a href="index.php">Home</a></li><!--
        --><li><a href="loginPage.php">Login</a></li><!--
        --><li><a href="registerPage.php">Register</a></li><!--
        --><li><a href="tutorPage.php">Tutors</a></li><!--
        --><li><a href="aboutPage.php">About Us</a></li><!--
        --><li><a href="contactPage.php">Contact Us</a></li>
      </ul>
    </nav>

</header>
<form action="" method= "post">

<section class="row">
<div class="grid">

  <div>
  <label for="to">To</label>
  <input type="text" name="to" id="to" value="<?php if (isset($_POST['to'])) echo htmlentities($_POST['to']); ?>" />
</div>

<div>
  <label for="subject">Subject</label>
<input type="text" name="subject" id="subject" value="<?php if (isset($_POST['subject'])) echo htmlentities($_POST['subject']); ?>" />
</div>

<div>
<textarea name="body" rows="20" cols="110"><?php if (isset($_POST['body'])) echo htmlentities($_POST['body']); ?></textarea>
</div>

<div>
  <input type="submit" value="send" />
</div>

</div>

</section>

</form>
<footer class="primary-footer container group">
  <small> &copy;</small>

  <nav class="nav">
    <ul>
      <li><a href="index.php">Home</a></li><!--
      --><li><a href="loginPage.php">Login</a><!--
      --><li><a href="tutorPage.php">Tutors</a><!--
      --><li><a href="registerPage.php">Register</a><!--
      --><li><a href="aboutPage.php">About Us</a><!--
      --><li><a href="contactPage.php">Contact Us</a>
      </ul>
  </nav>

</footer>

    </body>
    </html>

users.php:

<?php

function fetch_users_id($user_names){
  foreach($user_names as &$name) {
    $name = mysql_real_escape_string($name);
  }

  $results = mysql_query("SELECT id, Username FROM users WHERE Username IN ('" . implode("', '", $user_names) . "')");

  $names = array();

  while (($row = mysql_fetch_assoc($results)) !== false){
    $names[$row['Username']] = $row['id'];
  }

  return $names;
}

?>

Upvotes: 0

Views: 42

Answers (1)

skrilled
skrilled

Reputation: 5371

The function fetch_users_id does not exist in inboxPage.php

You must include or require users.php in inboxPage.php if you want to use that function within that file.

<?php
include("users.php");

Upvotes: 3

Related Questions