I'm trying to make a reply function so that when you receive a message you can reply to that specific message here is
messages.php where you can view 1 message at a time and reply to it
Here is reply.php
The problem is that when i reply the `to_id` in reply.php (see comment above) is always zero, how can i fix this?
messages.php where you can view 1 message at a time and reply to it
PHP Code:
<?php
require 'inc/head.inc';
require 'inc/header.inc';
if(isset($_SESSION['errors']))
{
if(is_array($_SESSION['errors']))
{
foreach($_SESSION['errors'] as $error)
print '<div class=dl>'.$error.'</div>';
}
else
print '<div class=dl2>'.$error.'</div>';
}
if(!$_SESSION['id'])
{
print 'You must <a href=index.php>login</a> to access this area';
}
else
{
$inboxSql = "SELECT * FROM messages WHERE to_id = '{$_SESSION['id']}' ORDER BY date DESC LIMIT 0,1";
$query = mysql_query($inboxSql, $con);
/* if(($rowsInbox = mysql_num_rows($query)) >= 1)
{
$sql = "SELECT * FROM messages WHERE to_id = '{$_SESSION['id']}' AND `read` = 0 ORDER BY date DESC LIMIT 0,1";
$query = mysql_query($sql);
}
else */
if(($rowsInbox = mysql_num_rows($query)) < 1)
print 'Your inbox looks clean';
else
while($rowInbox = mysql_fetch_array($query))
{
$_SESSION['from_id'] = $rowInbox['from_id'];
$_SESSION['inboxMsg'] = $rowInbox['msg'];
$userId = "SELECT * FROM users WHERE id = '{$_SESSION['from_id']}'";
$userFrom = mysql_query($userId, $con);
$userFetch = mysql_fetch_array($userFrom);
$_SESSION['from_id'] = $userFetch['nick'];
$_SESSION['msg_id'] = $rowInbox['id'];
print '<div class=nick>From : <a class=nick href=user.php?nick='.$_SESSION['from_id'].'>'.$_SESSION['from_id'].'</a></div>';
print '<div class=message>'.$_SESSION['inboxMsg'].'</div>';
$markRead = "UPDATE messages SET `read` = 1 WHERE id = '{$_SESSION['msg_id']}'";
$markQuery = mysql_query($markRead, $con);
print '<form action=reply.php method=post>
<input type=text name=reply size=15 maxlength=500>
<input class=button type=submit value=reply>
</form>';
$_SESSION['date'] = date("Y-m-d h:i:s");
}
}
require 'inc/foot.inc';
?>
PHP Code:
<?php
require 'inc/head.inc';
if(!isset($_SESSION['id']))
header('Location: login.php');
$errors = array();
if(empty($_POST['reply']))
$errors = 'To reply a message must be included';
if(strlen($_POST['reply']) > 500)
$errors = 'Message too long maximum is 500 characters';
if(count($errors))
{
$_SESSION['errors'] = $errors;
header('Location:messages.php');
}
else
{
$_SESSION['reply'] = mysql_real_escape_string(htmlentities(stripslashes($_POST['reply'])));
$sql = "INSERT INTO messages SET `id` = '{NULL}',
`from_id` = '{$_SESSION['id']}',
// `to_id` = '{$_SESSION['from_id']}',
`msg` = '{$_SESSION['reply']}',
`read` = '0',
`date` = '{$_SESSION['date']}'";
if(!$query = mysql_query($sql, $con))
$_SESSION['errors'] = mysql_error();
else
$_SESSION['errors'] = 'Message sent';
}
unset($_SESSION['from_id']);
unset($_SESSION['reply']);
unset($_SESSION['date']);
header('Location:messages.php');
?>
Comment