conan318 Posted May 2, 2011 Share Posted May 2, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/235336-reply-to-message/ Share on other sites More sharing options...
kney Posted May 2, 2011 Share Posted May 2, 2011 It's supposed to be: <?php echo '<a href="reply.php?username='". $getuname[0] ."'">reply</a><br />'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/235336-reply-to-message/#findComment-1209358 Share on other sites More sharing options...
Fadion Posted May 2, 2011 Share Posted May 2, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235336-reply-to-message/#findComment-1209359 Share on other sites More sharing options...
conan318 Posted May 2, 2011 Author Share Posted May 2, 2011 do u mean $getuname[0]= mysql_query("SELECT from_user FROM messages Where message_id='$message_id' AND to_user ='$myusername'"); or echo '<a href="reply.php?username=' . "$getuname[0]" . '">reply</a>'."<br/>"; or just set a variable called $getuname[0] Quote Link to comment https://forums.phpfreaks.com/topic/235336-reply-to-message/#findComment-1209360 Share on other sites More sharing options...
kney Posted May 2, 2011 Share Posted May 2, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/235336-reply-to-message/#findComment-1209362 Share on other sites More sharing options...
conan318 Posted May 2, 2011 Author Share Posted May 2, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235336-reply-to-message/#findComment-1209366 Share on other sites More sharing options...
trq Posted May 2, 2011 Share Posted May 2, 2011 Post your current code. Quote Link to comment https://forums.phpfreaks.com/topic/235336-reply-to-message/#findComment-1209368 Share on other sites More sharing options...
conan318 Posted May 2, 2011 Author Share Posted May 2, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/235336-reply-to-message/#findComment-1209372 Share on other sites More sharing options...
PFMaBiSmAd Posted May 2, 2011 Share Posted May 2, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235336-reply-to-message/#findComment-1209373 Share on other sites More sharing options...
Fadion Posted May 2, 2011 Share Posted May 2, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235336-reply-to-message/#findComment-1209374 Share on other sites More sharing options...
conan318 Posted May 2, 2011 Author Share Posted May 2, 2011 thanks heaps will be able to work this Quote Link to comment https://forums.phpfreaks.com/topic/235336-reply-to-message/#findComment-1209384 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.