Hi there,
I am currently trying to implement a PHP based PM system into my website. I have found the following tutorial as to what code to use:
I have downloaded the files and have edited them to the point that I can send New messages to users, and that users can read them, but if you look on the read_pm.php there is a form at the bottom where a user can reply to a message. This works fine on the example, but it gives me an error when I use it on my website, "An error occurred while sending the message."
I have attached images of my user table and also my pm table:
http://img.photobucket.com/albums/v225/Cjc1706/ScreenShot2012-02-25at170723.png
http://img.photobucket.com/albums/v225/Cjc1706/ScreenShot2012-02-25at170711.png
Also when a user logs in, it assigns a session variable called Userid, which is what is getting used in this script.
Im not too sure, but I think the error is below the part where it says "//We send the message and we change the status of the discussion to unread for the recipient "
Below is the code
<?php
//Start The PHP Session
session_start();
//Check if the user is logged in before allowing access to the page
require_once("checklog.php");
//Check if the user has admin status. If so, load the menu layout for the admin.
//if not, check if user is logged in, and load menu layout for logged in user
//otherwise, load menu layout for logged out user
if ($_SESSION['adminstatus'] == 1) {
include_once("home_start_logged_admin.php");
} elseif (isset($_SESSION['logged'])) {
include_once("home_start_logged.php");
} else {
include_once("home_start.php");
}
//Connect to the Database
require_once("db_connect.php");
mysql_select_db("a200332664_iwant") or die("<h1>Couldn't Locate Database</h1>");
//We check if the ID of the discussion is defined
if(isset($_GET['id']))
{
$id = intval($_GET['id']);
//We get the title and the narators of the discussion
$req1 = mysql_query('select title, user1, user2 from pm where id="'.$id.'" and id2="1"');
$dn1 = mysql_fetch_array($req1);
//We check if the discussion exists
if(mysql_num_rows($req1)==1)
{
//We check if the user have the right to read this discussion
if($dn1['user1']==$_SESSION['Userid'] or $dn1['user2']==$_SESSION['Userid'])
{
//The discussion will be placed in read messages
if($dn1['user1']==$_SESSION['Userid'])
{
mysql_query('update pm set user1read="yes" where id="'.$id.'" and id2="1"');
$user_partic = 2;
}
else
{
mysql_query('update pm set user2read="yes" where id="'.$id.'" and id2="1"');
$user_partic = 1;
}
//We get the list of the messages
$req2 = mysql_query('select pm.timestamp, pm.message, users.ID as userid, users.username from pm, users where pm.id="'.$id.'" and users.ID=pm.user1 order by pm.id2');
//We check if the form has been sent
if(isset($_POST['message']) and $_POST['message']!='')
{
$message = $_POST['message'];
//We remove slashes depending on the configuration
if(get_magic_quotes_gpc())
{
$message = stripslashes($message);
}
//We protect the variables
$message = mysql_real_escape_string(nl2br(htmlentities($message, ENT_QUOTES, 'UTF-8')));
//We send the message and we change the status of the discussion to unread for the recipient
if(mysql_query('insert into pm (id, id2, title, user1, user2, message, timestamp, user1read, user2read)values("'.$id.'", "'.(intval(mysql_num_rows($req2))+1).'", "", "'.$_SESSION['Userid'].'", "", "'.$message.'", "'.time().'", "", "")') and mysql_query('update pm set user'.$user_partic.'read="yes" where id="'.$id.'" and id2="1"'))
{
?>
<div class="message">Your message has successfully been sent.<br />
<a href="read_pm.php?id=<?php echo $id; ?>">Go to the discussion</a></div>
<?php
}
else
{
?>
<div class="message">An error occurred while sending the message.<br />
<a href="read_pm.php?id=<?php echo $id; ?>">Go to the discussion</a></div>
<?php
}
}
else
{
//We display the messages
?>
<div class="content">
<h1><?php echo $dn1['title']; ?></h1>
<table class="messages_table">
<tr>
<th class="author">User</th>
<th>Message</th>
</tr>
<?php
while($dn2 = mysql_fetch_array($req2))
{
?>
<tr>
<td class="author center"><?php
?><br /><a href="profile.php?id=<?php echo $dn2['userid']; ?>"><?php echo $dn2['username']; ?></a></td>
<td class="left"><div class="date">Sent: <?php echo date('m/d/Y H:i:s' ,$dn2['timestamp']); ?></div>
<?php echo $dn2['message']; ?></td>
</tr>
<?php
}
//We display the reply form
?>
</table><br />
<h2>Reply</h2>
<div class="center">
<form action="read_pm.php?id=<?php echo $id; ?>" method="post">
<label for="message" class="center">Message</label><br />
<textarea cols="40" rows="5" name="message" id="message"></textarea><br />
<input type="submit" value="Send" />
</form>
</div>
</div>
<?php
}
}
else
{
echo '<div class="message">You dont have the rights to access this page.</div>';
}
}
else
{
echo '<div class="message">This discussion does not exists.</div>';
}
}
else
{
echo '<div class="message">The discussion ID is not defined.</div>';
}
?>
I have been stressing out over this for the past few hours, and can not find any reason as to why this would not work.
Any help would be appreciated!
Thanks