Jump to content

Recommended Posts

i am making a message system which users can message each other. sending message works fine but having trouble with the reply message cant seem to get to_user value code so far

 

<?php
session_start();
$myusername=$_SESSION['myusername'];
require "database.php";

$messageid = $_GET['messageid'];
$message = mysql_query("SELECT * FROM messages WHERE message_id = '$messageid' AND to_user = '$myusername'");
$message=mysql_fetch_assoc($message);

// problem is here atm getuname returns Resource id  instead of the username iam trying to reply to //
$getuname= mysql_query("SELECT from_user FROM messages Where message_id='$message_id' AND to_user ='$myusername'");


echo "<h1>Title: ".$message['message_title']."</h1><br><br>";
echo "<h3>From: ".$message['from_user']."<br><br></h3>";
echo "<h3>Message: <br>".$message['message_contents']."<br></h3>";

echo '<form name="backfrm" method="post" action="inbox.php">';
echo '<input type="submit" value="Back to Inbox">';
// this where i need to the to user to carry over to the next page//
echo '<a href="reply.php?username=' . "$getuname" . '">reply</a>'."<br/>";
echo '</form>';
?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/235336-reply-to-message/
Share on other sites

mysql_query() returns a resource identifier, not an actual value. As you've done in the first query, you need to convert that resource into an array with mysql_fetch_assoc().

 

$getuname = mysql_query("SELECT from_user FROM messages Where message_id='$message_id' AND to_user ='$myusername'");
$uname_values = mysql_fetch_assoc($getuname);
$uname = $uname_values['from_user'];

 

Fixed that problem, your system doesn't seem to look right. If you have a message_id, there's no need to query for the username also. The query with a message ID should return the sender (to_user), receiver (actual user) and message. After that, you just swap places (to_user=>original sender, from_user=>original receiver) and quote the original message.

Link to comment
https://forums.phpfreaks.com/topic/235336-reply-to-message/#findComment-1209359
Share on other sites

I mean this:

 

<?php
session_start();
$myusername=$_SESSION['myusername'];
require "database.php";

$messageid = $_GET['messageid'];
$message = mysql_query("SELECT * FROM messages WHERE message_id = '$messageid' AND to_user = '$myusername'");
$message=mysql_fetch_assoc($message);

// problem is here atm getuname returns Resource id  instead of the username iam trying to reply to //
$getuname= mysql_query("SELECT from_user FROM messages Where message_id='$message_id' AND to_user ='$myusername'");

$uname = mysql_fetch_row($getuname);

echo "<h1>Title: ".$message['message_title']."</h1><br><br>";
echo "<h3>From: ".$message['from_user']."<br><br></h3>";
echo "<h3>Message: <br>".$message['message_contents']."<br></h3>";

echo '<form name="backfrm" method="post" action="inbox.php">';
echo '<input type="submit" value="Back to Inbox">';
// this where i need to the to user to carry over to the next page//
echo '<a href="reply.php?username='". $uname[0] ."'">reply</a><br />';
echo '</form>';
?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/235336-reply-to-message/#findComment-1209362
Share on other sites

mysql_query() returns a resource identifier, not an actual value. As you've done in the first query, you need to convert that resource into an array with mysql_fetch_assoc().

 

$getuname = mysql_query("SELECT from_user FROM messages Where message_id='$message_id' AND to_user ='$myusername'");
$uname_values = mysql_fetch_assoc($getuname);
$uname = $uname_values['from_user'];

 

Fixed that problem, your system doesn't seem to look right. If you have a message_id, there's no need to query for the username also. The query with a message ID should return the sender (to_user), receiver (actual user) and message. After that, you just swap places (to_user=>original sender, from_user=>original receiver) and quote the original message.

 

$getuname = mysql_query("SELECT from_user FROM messages Where message_id='$message_id' AND to_user ='$myusername'");
$uname_values = mysql_fetch_assoc($getuname);
$uname = $uname_values['from_user'];

 

just tryed that still returning resource  #18

Link to comment
https://forums.phpfreaks.com/topic/235336-reply-to-message/#findComment-1209366
Share on other sites

read_message.php

<?php
session_start();
$myusername=$_SESSION['myusername'];
require "database.php";

$messageid = $_GET['messageid'];
$message = mysql_query("SELECT * FROM messages WHERE message_id = '$messageid' AND to_user = '$myusername'");
$message=mysql_fetch_assoc($message);

$getuname = mysql_query("SELECT from_user FROM messages Where message_id='$message_id' AND to_user ='$myusername'");
$uname_values = mysql_fetch_assoc($getuname);
$uname = $uname_values['from_user'];




echo "<h1>Title: ".$message['message_title']."</h1><br><br>";
echo "<h3>From: ".$message['from_user']."<br><br></h3>";
echo "<h3>Message: <br>".$message['message_contents']."<br></h3>";

echo '<form name="backfrm" method="post" action="inbox.php">';
echo '<input type="submit" value="Back to Inbox">';
echo '<a href="reply.php?username=' . "$getuname" . '">reply</a>'."<br/>";
echo '</form>';
?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/235336-reply-to-message/#findComment-1209372
Share on other sites

Why do you even have that second query in your code? The first query is already getting everything about the message from the database.

 

Your second query is also using a $message_id variable that doesn't exist in the posted code and will never match anything in your database.

Link to comment
https://forums.phpfreaks.com/topic/235336-reply-to-message/#findComment-1209373
Share on other sites

conan318, you should really read what others have written first. Beginner or not, one should have the patience to read!

 

The show and reply functionality should work like this:

 

show.php

<?php
session_start();
$myusername = $_SESSION['myusername'];
require 'database.php';

$message_id = mysql_real_escape_string($_GET['messageid']); //or (int) $_GET['messageid']; if messageid is supposed to be an integer
$results = mysql_query("SELECT message_title, from_user, to_user, message_contents FROM messages WHERE message_id='$message_id'");
$values = mysql_fetch_assoc($results);

$message_title = stripslashes($values['message_title']);
$message_contents = stripslashes($values['message_contents']);
$from_user = $values['from_user'];
$to_user = $values['to_user']; //this should be equal to $myusername, or it's ID if it is an ID

echo "<h1>Title: $message_title</h1><br><br>";
echo "<h3>From: $from_user<br><br></h3>";
echo "<h3>Message: <br />$message_contents</h3>";

echo '<a href="inbox.php">Back to Inbox</a>';
echo '<a href="reply.php?messageid=' . $message_id . '">Reply</a>;
?>

 

In the same fashion, you make reply.php

<?php
session_start();
$myusername = $_SESSION['myusername'];
require 'database.php';

$message_id = mysql_real_escape_string($_GET['messageid']); //or (int) $_GET['messageid']; if messageid is supposed to be an integer
$results = mysql_query("SELECT message_title, from_user, to_user, message_contents FROM messages WHERE message_id='$message_id'");
$values = mysql_fetch_assoc($results);

$message_title = stripslashes($values['message_title']);
$message_contents = stripslashes($values['message_contents']);
$to_user = $values['from_user']; //this has become the TO:
$from_user = $values['to_user']; //this has become the FROM:

$message_contents = "\nOriginal Message:\n" . $message_contents;

echo '<form method="post" action="">';
echo '<input type="text" name="title" value="RE: ' . $message_title . '" />';
echo '<textarea name="contents">' . $message_contents . '</textarea>';
echo '</form>';

echo '<a href="inbox.php">Back to Inbox</a>';

if (isset($_POST['title'])) {
     $title = htmlentities($_POST['title'], ENT_QUOTES);
     $contents = htmlentities($_POST['contents'], ENT_QUOTES);
     $results = mysql_query("INSERT INTO messages (message_title, message_contents, from_user, to_user) VALUES ('$title', '$contents', '$from_user', '$to_user')");
     echo 'Message reply was sent successfuly!';
}
?>

 

It may not be a copy/paste solution, but that's the basic idea.

Link to comment
https://forums.phpfreaks.com/topic/235336-reply-to-message/#findComment-1209374
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.