Little problem with message system

Post here is you are having problems with any of the tutorials.
Post Reply
stefilix
Posts: 11
Joined: Thu Sep 06, 2012 10:26 pm

Little problem with message system

Post by stefilix »

I'm using a little different R/L system. So I gave my self a try.
I haven't done full tut on it yet.
Ok first, before I say what's that little problem, I'll show you what are those changes.
This is a way I'm using init.php file, I haven't enter anything inside from your your init.inc.php, I only connected
[syntax=php]require 'functions/private_message_func.php';[/syntax], so here is full script:
[syntax=php]
<?php
session_start();
error_reporting(0); //

require 'database/connect.php';
require 'functions/general_func.php';
require 'functions/users_func.php';
require 'functions/search_func.php';
require 'functions/blog_func.php';
require 'functions/private_message_func.php';

$current_file = explode('/', $_SERVER['SCRIPT_NAME']);
$current_file = end($current_file);

if (logged_in() === true) {
$session_user_id = $_SESSION['user_id'];
$user_data = user_data($session_user_id,'user_id', 'username', 'password' ,'first_name', 'last_name', 'gender','email', 'password_recover', 'type', 'allow_email', 'profile');

//Access to 1_login.php
if (user_active($user_data['username']) === false) {
session_destroy();
header('Location: index.php');
exit();
}

if ($current_file !== 'changepassword.php' && $user_data['password_recover'] == 1) {
header('Location: changepassword.php?force');
exit();
}

}

$errors = array();

?>
[/syntax]

Reason why I went this way(method), is because I really had no idea where to put some peace of your code, from your init.inc.php. So then, I created three new files: "Image --messages.php(which is in your case index.php), Image-- new_conversation.php
and view_conversation.php" which are all placed in base directory & private_message_func.php as I said earlier, at the start of the topic.

So, now I stuck at [part 09], first problem is, each time, I open page new_conversation.php it shows me as you can see on the picture http://img11.imageshack.us/img11/5598/newconversationphp.jpg Your message has been sent! Return to your Inbox, which I still haven't write.
Here is a script:
[syntax=php]<?php
include'core/init.php';
include 'includes/overall/header.php';
?>
<section class="section">
<div class="content_box"><h2>Messages</h2></div>
<div class="inner_content">
<p class="section_subname">New conversation</p>
<div id="global_data">
<?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']);

foreach ($user_names as &$name) {
$name = trim($name);
}

$user_ids = fetch_user_ids($user_names);

if (count($user_ids) !== count($user_names)) {
$errors[] = 'The following users could not be found: ' . implode(', ', array_diff($user_names, array_keys($user_ids)));
}
}

if (empty($_POST['subject'])) {
$errors = 'The subject cannot be empty.';
}

if (empty($_POST['body'])) {
$errors[] = 'The body cannot be empty.';
}

if (empty($errors)) {
create_conversation(array_unique($user_ids), $_POST['subject'], $_POST['body']);
}
}
if (empty($errors)) {
if (empty($errors)) {
echo '<div> Your message has been sent! <a href="messages.php">Return to your Inbox</a></div>';
} else {
foreach ($errors as $error) {
echo '<div>', $error, '</div>';
}
}
}
?>
<form action="" method="post" class="contact_form">
<table>
<tbody>
<tr>
<td><label for="to">To:</label></td>
<td><input type="text" name="to" maxlength="30" value="<?php if (isset($_POST['to'])) echo htmlentities($_POST['to']); ?>"></td>
</tr>
<tr>
<td><label for="subject">Subject:</label></td>
<td><input type="text" name="subject" maxlength="30" value="<?php if (isset($_POST['subject'])) echo htmlentities($_POST['subject']); ?>"></td>
</tr>
<tr>
<td class="body"><label for="message">Body:</label></td>
<td colspan="3"><textarea name="body" id="message" cols="60" rows="10"><?php if (isset($_POST['body'])) echo htmlentities($_POST['body']); ?></textarea></td>
</tr>
<tr>
<td></td>
<td colspan="1"><input type="submit" value="Send"></td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
</section>
<?php include 'includes/overall/footer.php';?>[/syntax]

Ok so, if you think this is a bad idea I went, just tell me what changes I should make now to my init.php, what peace of code ( if i should) pick from your init.inc.php and where to place it inside main.

Best regards,
Stefan
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Little problem with message system

Post by Temor »

I didn't go through all your code, so I won't comment on the suitability of doing things the way you did.
[syntax=php]if (empty($errors)) {
if (empty($errors)) {
echo '<div> Your message has been sent! <a href="messages.php">Return to your Inbox</a></div>';
} else {
foreach ($errors as $error) {
echo '<div>', $error, '</div>';
}
}
}[/syntax]

Changing the first statement to isset instead of empty should fix your problem.

[syntax=php]if (isset($errors)) {
if (empty($errors)) {
echo '<div> Your message has been sent! <a href="messages.php">Return to your Inbox</a></div>';
} else {
foreach ($errors as $error) {
echo '<div>', $error, '</div>';
}
}
}[/syntax]
stefilix
Posts: 11
Joined: Thu Sep 06, 2012 10:26 pm

Re: Little problem with message system

Post by stefilix »

Didn't fixed, it's still appearing.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Little problem with message system

Post by Temor »

It should not be appearing seeing as $errors is only set if $_POST['to'], $_POST['subject'] and $_POST['body'] is set.
[syntax=php]if (isset($_POST['to'], $_POST['subject'], $_POST['body'])) {
$errors = array();[/syntax]


are you refreshing using f5? that resends post data. Type the address again and see if the error disappears.
stefilix
Posts: 11
Joined: Thu Sep 06, 2012 10:26 pm

Re: Little problem with message system

Post by stefilix »

Thanks for a fast reply. Yes I did F5, ofc, but still msg is appearing..
p.s. Man I need to go afk, I'll back in 2h~.
stefilix
Posts: 11
Joined: Thu Sep 06, 2012 10:26 pm

Re: Little problem with message system

Post by stefilix »

Hey Temor,
Man you got any ideas, what's wrong ?
Z645
Posts: 33
Joined: Thu Jul 26, 2012 5:08 pm

Re: Little problem with message system

Post by Z645 »

Try:

[syntax=php]if (empty($errors)) {
if (empty($errors)) {
echo '<div>', $error, '</div>';
}else{
foreach ($errors as $error) {
echo '<div> Your message has been sent! <a href="messages.php">Return to your Inbox</a></div>';
}
}
}[/syntax]
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Little problem with message system

Post by Temor »

Z645 wrote:Try:

[syntax=php]if (empty($errors)) {
if (empty($errors)) {
echo '<div>', $error, '</div>';
}else{
foreach ($errors as $error) {
echo '<div> Your message has been sent! <a href="messages.php">Return to your Inbox</a></div>';
}
}
}[/syntax]

No, don't do that. That will print the success message for every error in the array.

I will take a closer look at this later. Be patient.
stefilix
Posts: 11
Joined: Thu Sep 06, 2012 10:26 pm

Re: Little problem with message system

Post by stefilix »

Thank you.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Little problem with message system

Post by Temor »

Well, try wrapping it in another if statement to see if the form has been submitted.

[syntax=php]
if (isset($_POST['to']){
if (isset($errors)) {
if (empty($errors)) {
echo '<div> Your message has been sent! <a href="messages.php">Return to your Inbox</a></div>';
} else {
foreach ($errors as $error) {
echo '<div>', $error, '</div>';
}
}
}
}[/syntax]
stefilix
Posts: 11
Joined: Thu Sep 06, 2012 10:26 pm

Re: Little problem with message system

Post by stefilix »

Wait, what do you want to wrap from where, i didn't understand you very well.
[syntax=php]
<?php
include'core/init.php';
include 'includes/overall/header.php';
?>
<section class="section">
<div class="content_box"><h2>Messages</h2></div>
<div class="inner_content">
<p class="section_subname">New conversation</p>
<div id="global_data">
<?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 does not look valid.';
} else {
$user_names = explode(',', $_POST['to']);

foreach ($user_names as &$name) {
$name = trim($name);
}

$user_ids = fetch_user_ids($user_names);

if (count($user_ids) !== count($user_names)) {
$errors[] = 'The following users could not be found: ' . implode(', ', array_diff($user_names, array_keys($user_ids)));
}
}

if (empty($_POST['subject'])) {
$errors = 'The subject cannot be empty.';
}

if (empty($_POST['body'])) {
$errors[] = 'The body cannot be empty.';
}

if (empty($errors)) {
create_conversation(array_unique($user_ids), $_POST['subject'], $_POST['body']);
}
}
if (isset($errors)) {
if (empty($errors)) {
echo '<div> Your message has been sent! <a href="messages.php">Return to your Inbox</a></div>';
} else {
foreach ($errors as $error) {
echo '<div>', $error, '</div>';
}
}
}
?>
<form action="" method="post" class="contact_form">
<table>
<tbody>
<tr>
<td><label for="to">To:</label></td>
<td><input type="text" name="to" maxlength="30" value="<?php if (isset($_POST['to'])) echo htmlentities($_POST['to']); ?>"></td>
</tr>
<tr>
<td><label for="subject">Subject:</label></td>
<td><input type="text" name="subject" maxlength="30" value="<?php if (isset($_POST['subject'])) echo htmlentities($_POST['subject']); ?>"></td>
</tr>
<tr>
<td class="body"><label for="message">Body:</label></td>
<td colspan="3"><textarea name="body" id="message" cols="60" rows="10"><?php if (isset($_POST['body'])) echo htmlentities($_POST['body']); ?></textarea></td>
</tr>
<tr>
<td></td>
<td colspan="1"><input type="submit" value="Send"></td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
</section>
<?php include 'includes/overall/footer.php';?>
[/syntax]
stefilix
Posts: 11
Joined: Thu Sep 06, 2012 10:26 pm

Re: Little problem with message system

Post by stefilix »

Alright, seem there's no solution for this, I'll go over again.
Could you just tell me where I can add your peace of code from your init.inc.php to my
init.php, to not cause any problems?

my init.php
[syntax=php]
<?php
session_start();
error_reporting(0); //

require 'database/connect.php';
require 'functions/general_func.php';
require 'functions/users_func.php';
require 'functions/search_func.php';
require 'functions/blog_func.php';

$current_file = explode('/', $_SERVER['SCRIPT_NAME']);
$current_file = end($current_file);

if (logged_in() === true) {
$session_user_id = $_SESSION['user_id'];
$user_data = user_data($session_user_id,'user_id', 'username', 'password' ,'first_name', 'last_name', 'gender','email', 'password_recover', 'type', 'allow_email', 'profile');

//Access to 1_login.php
if (user_active($user_data['username']) === false) {
session_destroy();
header('Location: index.php');
exit();
}

if ($current_file !== 'changepassword.php' && $user_data['password_recover'] == 1) {
header('Location: changepassword.php?force');
exit();
}

}

$errors = array();

?>

[/syntax]
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Little problem with message system

Post by Temor »

I thought it was pretty obvious which part you should wrap and in what, seeing as I posted the exact code.

[syntax=php]if (isset($_POST['to']){
if (isset($errors)) {
if (empty($errors)) {
echo '<div> Your message has been sent! <a href="messages.php">Return to your Inbox</a></div>';
} else {
foreach ($errors as $error) {
echo '<div>', $error, '</div>';
}
}
}
}[/syntax]

is equal to this:
[syntax=php] if (isset($errors)) {
if (empty($errors)) {
echo '<div> Your message has been sent! <a href="messages.php">Return to your Inbox</a></div>';
} else {
foreach ($errors as $error) {
echo '<div>', $error, '</div>';
}
}
}[/syntax]

inside this:
[syntax=php]if (isset($_POST['to']){

}[/syntax]
stefilix
Posts: 11
Joined: Thu Sep 06, 2012 10:26 pm

Re: Little problem with message system

Post by stefilix »

Ye thanks for that, that fixed problem. I've been rusty then..
However, I've just completed full tutorial, and I came across with two problems only.
In view_conversation.php, when I want to Add Message, it do add a message below, but it's not showing text from a message submited. It shows only (by who/time), but not an that text below.
Image
I've also checked on mPAdmin, if it exists there maybe, but no, it just shows same values as for site page.
Image
Could you please check func and view_conversation.php, I was looking for that error for 30m+ couldn't see :? ...
pm_system_func.php
[syntax=php]
<?php

// Fetches a summary of the conversations.
function fetch_conversation_summary() {
$sql = "SELECT
`conversations`.`conversation_id`,
`conversations`.`conversation_subject`,
MAX(`conversations_messages`.`message_date`) AS `conversation_last_reply`,
MAX(`conversations_messages`.`message_date`) > `conversations_members`.`conversation_last_view` AS `conversation_unread`
FROM `conversations`
LEFT JOIN `conversations_messages` ON `conversations`.`conversation_id` = `conversations_messages`.`conversation_id`
INNER JOIN `conversations_members` ON `conversations`.`conversation_id` = `conversations_members`.`conversation_id`
WHERE `conversations_members`.`user_id` = {$_SESSION['user_id']}
AND `conversations_members`.`conversation_deleted` = 0
GROUP BY `conversations`.`conversation_id`
ORDER BY `conversation_last_reply` DESC";

$result = mysql_query($sql);

$conversations = array();

while (($row = mysql_fetch_assoc($result)) !== false) {
$conversations[] = array(
'id' => $row['conversation_id'],
'subject' => $row['conversation_subject'],
'last_reply' => $row['conversation_last_reply'],
'unread_messages' => ($row['conversation_unread'] == 1),
);
}

return $conversations;
}

// Fetches all of the messages in the given conversation.
function fetch_conversation_messages($conversation_id) {
$conversation_id = (int)$conversation_id;

$sql = "SELECT
`conversations_messages`.`message_date`,
`conversations_messages`.`message_date` > `conversations_members`.`conversation_last_view` AS `message_unread`,
`conversations_messages`.`message_text`,
`users`.`username`
FROM `conversations_messages`
INNER JOIN `users` ON `conversations_messages`.`user_id` = `users`.`user_id`
INNER JOIN `conversations_members` ON `conversations_messages`.`conversation_id` = `conversations_members`.`conversation_id`
WHERE `conversations_messages`.`conversation_id` = {$conversation_id}
AND `conversations_members`.`user_id` = {$_SESSION['user_id']}
ORDER BY `conversations_messages`.`message_date` DESC";

$result = mysql_query($sql);

$messages = array();

while (($row = mysql_fetch_assoc($result)) !== false){
$messages[] = array(
'date' => $row['message_date'],
'unread' => $row['message_unread'],
'text' => $row['message_text'],
'username' => $row['username']
);
}

return $messages;
}
// Sets the last view time to the current time for the given conversation.
function update_conversation_last_view($conversation_id) {
$conversation_id = (int)$conversation_id;

$sql ="UPDATE `conversations_members`
SET `conversation_last_view` = UNIX_TIMESTAMP()
WHERE `conversation_id` = {$conversation_id}
AND `user_id` = {$_SESSION['user_id']}";

mysql_query($sql);
}

// Creates a new conversation, making the given users a member.
function create_conversation($user_ids, $subject, $body) {
$subject = mysql_real_escape_string(htmlentities($subject));
$body = mysql_real_escape_string(htmlentities($body));

mysql_query("INSERT INTO `conversations` (`conversation_subject`) VALUES ('{$subject}')");

$conversation_id = mysql_insert_id();

$sql = "INSERT INTO `conversations_messages` (`conversation_id`, `user_id`, `message_date`, `message_text`)
VALUES ({$conversation_id}, {$_SESSION['user_id']}, UNIX_TIMESTAMP(), '{$body}')
";

mysql_query($sql);

$values = array("({$conversation_id}, {$_SESSION['user_id']}, UNIX_TIMESTAMP(), 0)");

foreach ($user_ids as $user_id) {
$user_id = (int) $user_id;

$values[] = "({$conversation_id}, {$user_id}, 0, 0)";
}

$sql = "INSERT INTO `conversations_members` (`conversation_id`, `user_id`, `conversation_last_view`, `conversation_deleted`)
VALUES " . implode(", ", $values);

mysql_query($sql);
}

// Checks to see if the given user is a number of the given conversation.
function validate_conversation_id($conversation_id) {
$conversation_id = (int)$conversation_id;

$sql = "SELECT COUNT(1)
FROM `conversations_members`
WHERE `conversation_id` = {$conversation_id}
AND `user_id` = {$_SESSION['user_id']}
AND `conversation_deleted` = 0";

$result = mysql_query($sql);

return (mysql_result($result, 0) == 1);
}

// Adds a message to the given conversation.
function add_conversation_message($conversation_id, $text) {
$conversation_id = (int)$conversation_id;
$text = mysql_real_escape_string(htmlentities($text));

$sql = "INSERT INTO `conversations_messages` (`conversation_id`, `user_id`, `message_date`, `message_text`)
VALUES ({$conversation_id}, {$_SESSION['user_id']}, UNIX_TIMESTAMP(), '{$text}')";

mysql_query($sql);
}

// Deletes (or marks as deleted) a given conversation.
function delete_conversation($conversation_id) {
$conversation_id = (int)$conversation_id;

$sql = "SELECT DISTINCT `conversation_deleted`
FROM `conversations_members`
WHERE `user_id` != {$_SESSION['user_id']}
AND `conversation_id` = {$conversation_id}";

$result = mysql_query($sql);

if (mysql_num_rows($result) === 1 && mysql_result($result, 0) == 1) {
mysql_query("DELETE FROM `conversations` WHERE `conversation_id` = {$conversation_id}");
mysql_query("DELETE FROM `conversations_members` WHERE `conversation_id` = {$conversation_id}");
mysql_query("DELETE FROM `conversations_messages` WHERE `conversation_id` = {$conversation_id}");
} else {
$sql = "UPDATE `conversations_members`
SET `conversation_deleted` = 1
WHERE `conversation_id` = {$conversation_id}
AND `user_id` = {$_SESSION['user_id']}";

mysql_query($sql);
}
}
?>[/syntax]
view_conversation.php
[syntax=php]
<?php
include'core/init.php';
protect_page();
include 'includes/overall/header.php';
?>
<section class="section">
<div class="content_box"><h2>Messages</h2></div>
<div class="inner_content">
<p class="section_subname">View conversation</p>
<div id="global_data">
<div>
<a href="messages.php"> Messages</a>
</div>
<?php
$errors = array();

$valid_conversation = (isset($_GET['conversation_id']) &&
validate_conversation_id($_GET['conversation_id']));

if ($valid_conversation === false) {
$errors[] = 'Invalid conversation ID.';
}

if (isset($_POST['message'])) {
if (empty($_POST['message'])) {
$errors[] = 'You must enter a message.';
}

if (empty($errors)) {
add_conversation_message($_GET['conversation_id'], $_GET['message']);
}
}

if (empty($errors) === false) {
foreach($errors as $error) {
echo '<div class"msg_error">', $error, '</div>';
}
}

if ($valid_conversation) {
if (isset($_POST['message'])) {
update_conversation_last_view($_GET['conversation_id']);
$messages = fetch_conversation_messages($_GET['conversation_id']);
} else {
$messages = fetch_conversation_messages($_GET['conversation_id']);
update_conversation_last_view($_GET['conversation_id']);
}
?>
<form action="" method="post">
<div>
<textarea name="message"rows="10" cols="70"></textarea>
</div>
<div>
<input type="submit" value="Add Message">
</div>
</form><br>
<div class="messages">
<?php
foreach ($messages as $message) {
?>
<div class="message <?php if ($message['unread']) echo 'unread'; ?>">
<p class="name"><?php echo $message['username']; ?> (<?php echo date('d/m/Y H:i:s', $message['date']); ?>)</p>
<p class="text"><?php echo $message['text']; ?></p>
</div><br>
<?php
}
?>
</div>
<?php
}
?>
</div>
</div>
</section>
<?php include 'includes/overall/footer.php';?>
[/syntax]
I point that problem could be in view_conversation.php but still haven't seen anything strange..
------------------------------------------------------------------------------------------------------------------------------------------------------
Second thing, with deleting conversations.
Function for deleting conversation is fully functional, but for anchor tag (where's delete conversation),
isn't working, any idea ?
[syntax=php]
<a href="message.php?delete_conversation=<?php echo $conversation['id']; ?>">[x]</a>
[/syntax]
[syntax=php]
<div class="conversation <?php if ($conversation['unread_messages']) echo 'unread'?>">
<h3>
<a href="message.php?delete_conversation=<?php echo $conversation['id']; ?>">[x]</a>
<a href="view_conversation.php?conversation_id=<?php echo $conversation['id']; ?>"><?php echo $conversation['subject']; ?></a>
</h3>
<p>Last Reply: <?php echo date('d/m/y H:i:s', $conversation['last_reply']); ?></p>
</div>
[/syntax]
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Little problem with message system

Post by Temor »

Line 30, view_conversation.php
[syntax=php]add_conversation_message($_GET['conversation_id'], $_GET['message']);[/syntax]

you are using $_GET['message'] where you should be using $_POST['message']
stefilix
Posts: 11
Joined: Thu Sep 06, 2012 10:26 pm

Re: Little problem with message system

Post by stefilix »

Aww.. what a mistake... it was so logical.
Btw give me idea for what I wrote in last post, how to remove conversations?
Or I should just do a new func, which will look something like this:
[syntax=php]
function delete($table, $id) {
$table = mysql_real_escape_string($table);
$id = (int)$id;

mysql_query("DELETE FROM `{$table}` WHERE `id` = {$id}");
}
[/syntax]
And then, create new file delete_conversation.php, which will contain:
[syntax=php]
<?php
include'core/init.php';

if (!isset($_GET['id'])) {
header('Location: messages.php');
die();
}

delete_conversation('conversations', $_GET['id']);

header('Location: messages.php');
die();
?>
[/syntax]
What do you suggest me?
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Little problem with message system

Post by Temor »

Jacek covered deleting conversations in the tutorial series.
stefilix
Posts: 11
Joined: Thu Sep 06, 2012 10:26 pm

Re: Little problem with message system

Post by stefilix »

Yes, I have working function and this anchor tag which is not deleting conversation[syntax=php]<a href="delete_conversation.php?conversation_id=<?php echo $conversation['id']; ?>"></a> [/syntax]
Image
When I hover over X icon, you can see that it shows as well conversation_id=13.
I've tried also view_conversation.php?delete_conversation=conversation_id={$id}, but not working.
Any ideas?
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Little problem with message system

Post by Temor »

You've probably solved this already, but you're naming the function delete but you're calling delete_conversation.
Post Reply