我对函数的调用显然是不正确的,我知道它不是,但不清楚为什么

时间:2016-04-06 21:07:14

标签: php

我正在尝试制作一个私人收件箱功能的教程,一切都与我每次尝试按下发送时出现500服务器错误的事实相去甚远。我已经检查了可能导致此错误的日志,这是我收到的内容: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

我已检查过该功能,看是否有任何不合适的地方,但无法发现任何可能将其丢掉的东西。

欣赏另一双眼睛,帮助我看看我在这里做错了什么。

提前致谢!

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;
}

?>

1 个答案:

答案 0 :(得分:3)

inboxPage.php中不存在函数fetch_users_id

如果您想在该文件中使用该功能,则必须在 inboxPage.php 中加入或要求 users.php

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