Problem replying to PM's

Started on the old forum in September 2011 · view the original in the archive · old links redirect here

#debugging #error #mysql-database #mysql-error #php

1 reply · 434 views · started 15 years ago · 1 min read

2 participants
s3nzo Developer · 156 posts #1

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

<?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';
?>

Here is reply.php

<?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');
?>

The problem is that when i reply the to_id in reply.php (see comment above) is always zero, how can i fix this?

CreativityKills Lead · 833 posts #2

IMHO your codes a bit dirty. Meaning im sure there are cleaner ways to achieve what you want. Then u r using the _SESSION global var a bit 2 much. I would suggest you move your session error check to a php class or function. You'll avoid redundant code and keep it cleaner. Using .inc extension is horrible practice as visiting the file (if the AddHandler isnt specified to apache) wil reveal the source.

Added after 3 minutes:

...and oh i saw u do something like...

include 'head.inc';

// and later...
header('Location: login.php');

the problem is if you have outputted anything as little as a whitespace in 'head.inc' you will get a cannot modify header error. You should really fink of code architecture before embarking in it. Warned you of it before.

so that said, i knw u'll stil continue so use $_GET/$_POST instead of $_SESSION

👍 1
Share

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close