PM- System---funktion gibt fehler aus

Henselmania

Mitglied
Hallo Forum,

ich bin grade dabei in meine Seite ein PM- System zu integrieren...
Nun weiß ich aber leider nicht mehr weiter.... Vieleicht kann einer von euch mir helfen ?

Folgendes Probelm:
Das PM-System besteht aus 2 Dateien: pm.php und funtions.php

pm.php
PHP:
<?php 
session_start(); 
$_SESSION['userID'] = 1; 
require_once("functions.php"); // include the functions we just wrote 
define('DATE_FORMAT', 'Y-m-d g:ia'); // define our date format variable 

$HOST = 'localhost'; // Set to your database server 
$USER = 'root';  // Set to your database user 
$PASS = '';  // Set to your database password 
$NAME = 'pm';  // Set to the appropriate database 

$conn = mysql_connect($HOST, $USER, $PASS); 
if (!$conn) die('Error connecting to server!'); 
mysql_select_db($NAME, $conn) or die('Error selecting database'); 
 

// Process the message once it has been sent 
if (isset($_POST['newMessage'])) { 
  // Escape and prepare our variables for insertion into the database 
  // This is also where you would run any sort of editing, such as BBCode parsing 
  $to   = mysql_real_escape_string($_POST['to']); 
  $from = $_SESSION['userID']; 
  $sub  = mysql_real_escape_string($_POST['subject']); 
  $msg  = mysql_real_escape_string($_POST['message']); 
     
  // Handle all your specific error checking here 
  if (empty($to) || empty($sub) || empty($msg)) { 
    $error = "<p>You must select a recipient and provide a subject and message.</p>\n"; 
  } else { 
    // Notice carefully how we only have to provide the five values we previously discussed 
    $sql = "INSERT INTO myPMs (to_id, from_id, time_sent, subject, message) VALUES ('$to', '$from', NOW(), '$sub', '$msg')"; 
    if (!mysql_query($sql)) { 
      $error = "<p>Could not send message!</p>\n"; 
    } else { 
      $message = "<p>Message sent successfully!</p>\n"; 
    } 
  } 
} 

echo isset($error) ? $error : ''; 
echo isset($message) ? $message : ''; 

echo "<form name=\"newMessage\" action=\"\" method=\"post\">\n"; 
echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n"; 
echo "<tr>\n"; 
echo "<td>To:</td>\n"; 
echo "<td><select name=\"to\">\n"; 
echo "<option value=\"\"></option>\n"; 

// Collect and loop through all usernames that are not the current user 
$sql = mysql_query("SELECT * FROM users WHERE id != '$_SESSION[userID]' ORDER BY username"); 
if (mysql_num_rows($sql) > 0) { 
  while ($x = mysql_fetch_assoc($sql)) echo "<option value=\"$x[id]\">$x[username]</option>\n"; 
} 

echo "</select></td>\n"; 
echo "</tr>\n"; 
echo "<tr>\n"; 
echo "<td>Subject:</td>\n"; 
echo "<td><input type=\"text\" name=\"subject\" value=\"" . (isset($error) ? $_POST['subject'] : '') . "\" maxlength=\"50\" /></td>\n"; 
echo "</tr>\n"; 
echo "<tr>\n"; 
echo "<td>Message:</td>\n"; 
echo "<td>\n"; 
echo "<textarea name=\"message\" cols=\"\" rows=\"\">" . (isset($error) ? $_POST['message'] : '') . "</textarea>\n"; 
echo "</td>\n"; 
echo "</tr>\n"; 
echo "<tr>\n"; 
echo "<td></td>\n"; 
echo "<td><input type=\"submit\" name=\"newMessage\" value=\"Send\" /></td>\n"; 
echo "</tr>\n"; 
echo "</table>\n"; 
echo "</form>\n"; 
?> 

<?php 
// Returns an array of all my messages 
// Defaults to current user and INBOX messages 
// Make the $sent parameter "true" to retrieve sent messages 
function getMyMessageList($id = '', $sent = false) { 
  $id = empty($id) ? $_SESSION['userID'] : $id; 

  $where = $sent ? "from_id = '$id'" : "to_id = '$id'"; 
  $join  = $sent ? "p.to_id = u.id" : "p.from_id = u.id"; 

  $messages = array(); 

  // Construct query 
  $sql = "SELECT p.id, to_id, from_id, time_sent, subject, message, opened,  
          time_opened, username FROM myPMs p LEFT JOIN users u  
          ON $join WHERE $where order by time_sent DESC"; 
  $res = mysql_query($sql) or die(mysql_error()); 

  // If there are records, populate the array to return 
  if (mysql_num_rows($res) > 0) { 
    while ($row = mysql_fetch_assoc($res)) { 
      $messages[] = $row; 
    } 
  } 

  // Return the array of messages to the caller 
  return $messages; 
} 

// Gets a specific message, but only if it corresponds to the 
// provided user ID and type of message provided. Once again, we 
// default to the current user and INCOMING messages. 
function getMyMessage($id, $user = '', $sent = false) { 
  $user = empty($user) ? $_SESSION['userID'] : $user; 

  $where = $sent ? "from_id = '$user'" : "to_id = '$user'"; 
  $join  = $sent ? "p.to_id = u.id" : "p.from_id = u.id"; 

  // Construct query 
  $sql = "SELECT p.id, to_id, from_id, time_sent, subject, message, opened, 
          time_opened, username FROM myPMs p LEFT JOIN users u 
          ON $join WHERE $where AND p.id = '$id'"; 

  $res = mysql_query($sql); 

  // If there is a row found, return it as an associative array, 
  // otherwise, return false to show we didn't find anything. 
  if (mysql_num_rows($res) == 1) { 
    return mysql_fetch_assoc($res); 
  } else return false; 
} 
if (isset($_GET['action']) && $_GET['action'] == 'send') { 
  $title = "Send Message"; 
  require('new.php'); 
  exit(); 
} 
$folder = isset($_GET['folder']) ? $_GET['folder'] : 'inbox'; 

switch($folder) { 
  case 'sent': 
    // Show my sent messages 
    $title = "Sent Messages"; 

    // Notice we set the second parameter to "true" to pull sent messages 
    $myMessages = getMyMessageList('', true); 

    // Set the columns we will be using for our display 
    $cols = array('To', 'Subject', 'Time'); 
    break; 

  default: 
    // Show our inbox 
    // Notice we are setting the same variables as above 
    $title = "Inbox"; 
    $folder = "inbox"; // This is in case we have something errant entered 
    $myMessages = getMyMessageList(); 
    $cols = array('From', 'Subject', 'Time', 'Del'); 
} 

// This is so we know how many columns we actually have 
$span = count($cols); 
?> 
<title><?php echo $title; ?> ~ PM System</title> 
<?php 
if (isset($_GET['id'])) { 
  $id = $_GET['id']; 
  switch($folder) { 
    case 'sent': 
      $msg  = getMyMessage($id, '', true); 
      $back = "?folder=sent"; // Set my link back to Sent Messages 
      $from = "To"; 
      break; 

    case 'inbox': 
      $msg  = getMyMessage($id); 
      $back = "?folder=inbox"; 
      $from = "From"; 
      break; 

    // Obviously, if you choose, you can easily add more boxes without 
    // too much difficulty. 
  } 

  // Output a "back" link 
  echo "<p><a href=\"$back\">&laquo; Back</a></p>\n"; 

  // If there is no message returned, we have an error 
  if (!$msg) { 
    echo "<p>Invalid message requested</p>\n"; 
  } else { 
// Define our variables (removing slashes) 
    // Add any other formating you like here (including BBCode, etc) 
    $user = stripslashes($msg['username']); 
    $date = date(DATE_FORMAT, strtotime($msg['time_sent'])); // Notice our defined constant 
    $subject = stripslashes($msg['subject']); 
    $message = nl2br(stripslashes($msg['message'])); 
    $opened  = $msg['opened']; 

    // Mark a received message "read" when it's opened 
    if ($msg['to_id'] == $_SESSION['userID'] && $opened == 'n') { 
      $sql = "UPDATE myPMs SET opened = 'y', time_opened = NOW() WHERE id = '$id'"; 
      mysql_query($sql); 
    } 
     
    // Output our message 
    echo "<h3>$subject</h3>\n"; 
    echo "<p>$from <b>$user</b><br />on $date</p>\n"; 
    echo "<p>$message</p>\n"; 
  } 

} else {
 echo "<p><a href=\"?folder=inbox\">Inbox</a> |\n"; 
  echo "<a href=\"?folder=sent\">Sent Messages</a></p>\n"; 

  // If we're in the inbox, show a Create link 
  if ($folder == 'inbox') { 
    echo "<p><a href=\"?action=send\">Create New Message</a></p>\n"; 
  } ;


  // You'll notice throughout this snippet that we're only displaying 
  // the delete messages form if we are in the inbox. 
  if ($folder == 'inbox') { 
    echo "<form name=\"deleteMessages\" action=\"\" method=\"post\">\n"; 
  } 

  echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n"; 
  echo "<tr>\n"; 

  // Create our headings with the column names we defined previously 
  echo "<th>" . implode("</th>\n<th>", $cols) . "</th>\n"; 
  echo "</tr>\n"; 

  // Make sure we have some messages to display 
  if (count($myMessages) > 0) { 

    // Loop through each message and display it on a row 
    foreach ($myMessages as $msg) { 

      // Determine to show the message as read or unread 
      $class = $msg['opened'] == 'y' ? 'read' : 'unread'; 
      $date = date(DATE_FORMAT, strtotime($msg['time_sent'])); 

      echo "<tr class=\"$class\">\n"; 
      echo "<td>$msg[username]</td>\n"; 

      // Hyperlink subject to display message 
      echo "<td><a href=\"?folder=$folder&amp;id=$msg[id]\">$msg[subject]</a></td>\n"; 
      echo "<td>$date<?td>\n"; 

      // Checkbox to select which messages to delete 
      if ($folder == 'inbox') { 
        echo "<td><input type=\"checkbox\" name=\"del[]\" value=\"$msg[id]\" /></td>\n"; 
      } 
      echo "</tr>\n"; 
    } 

    // More of our delete form 
    // This will be our submit button to delete selected entries 
    if ($folder == 'inbox') { 
      echo "<tr class=\"deleteRow\">\n"; 
      echo "<td colspan=\"$span\"><input type=\"submit\" name=\"delete\" value=\"Delete Selected\" /></td>\n"; 
      echo "</tr>\n"; 
    } 
  } else { 
    // We have no messages in this box. 
    echo "<tr>\n"; 
    echo "<td colspan=\"$span\"></p>You have no messages</p></td>\n"; 
    echo "</tr>\n"; 
  } 
  
  echo "</table>\n"; 

  if ($folder == 'inbox') { 
    echo "</form>\n"; 
  } 

} // End Script 
if (isset($_POST['delete']) && count($_POST['del']) > 0) { 
  // Make sure they are only attempting to delete their own messages 
  $sql = "DELETE FROM myPMs WHERE id IN ('" . imlode("','", $_POST['del']) . "') AND to_id = '$_SESSION[userID]'"; 
  if (!mysql_query($sql)) { 
    // Could not delete selected messages 
  } else { 
    // Successfully deleted messages 
  } 
}

funtions.php
PHP:
<?php 
// Returns an array of all my messages 
// Defaults to current user and INBOX messages 
// Make the $sent parameter "true" to retrieve sent messages 
function getMyMessageList($id = '', $sent = false) { 
 $id = empty($id) ? $_SESSION['userID'] : $id; 

  $where = $sent ? "from_id = '$id'" : "to_id = '$id'"; 
  $join  = $sent ? "p.to_id = u.id" : "p.from_id = u.id"; 

  $messages = array(); 

  // Construct query 
  $sql = "SELECT p.id, to_id, from_id, time_sent, subject, message, opened,  
          time_opened, username FROM myPMs p LEFT JOIN users u  
          ON $join WHERE $where order by time_sent DESC"; 
  $res = mysql_query($sql) or die(mysql_error()); 

  // If there are records, populate the array to return 
  if (mysql_num_rows($res) > 0) { 
    while ($row = mysql_fetch_assoc($res)) { 
      $messages[] = $row; 
    } 
  } 

  // Return the array of messages to the caller 
  return $messages; 
} 

// Gets a specific message, but only if it corresponds to the 
// provided user ID and type of message provided. Once again, we 
// default to the current user and INCOMING messages. 
function getMyMessage($id, $user = '', $sent = false) { 
  $user = empty($user) ? $_SESSION['userID'] : $user; 

  $where = $sent ? "from_id = '$user'" : "to_id = '$user'"; 
  $join  = $sent ? "p.to_id = u.id" : "p.from_id = u.id"; 

  // Construct query 
  $sql = "SELECT p.id, to_id, from_id, time_sent, subject, message, opened, 
          time_opened, username FROM myPMs p LEFT JOIN users u 
          ON $join WHERE $where AND p.id = '$id'"; 

  $res = mysql_query($sql); 

  // If there is a row found, return it as an associative array, 
  // otherwise, return false to show we didn't find anything. 
  if (mysql_num_rows($res) == 1) { 
    return mysql_fetch_assoc($res); 
  } else return false; 
} 
?>

Nun ist mein Probelm das wenn ich die Datei pm.php aufrufe immer eine Fehlermeldung bekomme...
Fatal error: Cannot redeclare getmymessagelist() (previously declared in D:\xampp\htdocs\pm\pm.php:80) in D:\xampp\htdocs\pm\functions.php on line 28

Kann mir jemand sagen wo mein Fehler liegt ?

Danke.
 
Hallo Henselmania,

bin zwar nicht der fitteste, aber durch:

PHP:
require_once("functions.php"); // include the functions we just wrote
lädst du zwei funktionen, diese sind aber in pm.php noch mal drin.

Gruß Robert
 
Zurück