shadowayex Posted May 12, 2008 Share Posted May 12, 2008 I'm building a messaging system for a website. I think I have the basic concept down and whatnot. The code looks like it should work. It doesn't. My whole page looks like this:messages.php: <?php session_start();$link = mysql_connect('host', 'user', 'password') or die('Could not connect: ' . mysql_error());mysql_select_db('database') or die('Could not select database');$user=$_SESSION['user'];if(mode==sendmsg){$to=mysql_real_escape_string($_POST['to']);$subject=mysql_real_escape_string($_POST['subject']);$message=mysql_real_escape_string($_POST['message']);$id=mysql_query("SELECT * FROM messages");$id=mysql_num_rows($id)+1;$check=mysql_query("SELECT Username FROM players WHERE Username='$to'"); if(mysql_num_rows($check)==1) { $sql = mysql_query("INSERT INTO messages (Id, To, From, Subject, Message) VALUES ('$id', '$to', '$user', '$subject', '$message')"); if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } else { header('location: messages.php?mode=inbox'); } } else { echo "User does not exist. Try again."; echo "<form action='messages.php?mode=sendmsg' method='post'>"; echo "<div>"; echo "To: <input type='text' name='to' value='' /><br />"; echo "Subject: <input type='text' name='subject' value='' /><br />"; echo "Message:<br />"; echo "<textarea rows='10' cols='50' name='message'></textarea><br />"; echo "<input type='submit' name='submit' value='Send' />"; }}mysql_close($link);?><html><head><title>Messages</title><style type="text/css">#menu1 a {color:black;background-color:white;text-decoration:none;text-indent:1ex;}#menu1 a:active {color:black;text-decoration:none;}#menu1 a:hover {color:black;background-color:#FFFF99}#menu1 a:visited {color:black;text-decoration:none;}</style><script src="mmenu.js" type="text/javascript"></script><script src="menuItems.js" type="text/javascript"></script></head><body><div>Welcome <?php echo $user; ?>!<br /><a href="home.php">Home</a> | <a href="messages.php?mode=newmsg">Send New Message</a> | <a href="messages.php?mode=viewinbox">Inbox</a> | <a href="messages.php?mode=viewsent">Outbox</a> | <a href="logout.php">Log Out</a></div><div><?php session_start();$link = mysql_connect('host', 'user', 'password') or die('Could not connect: ' . mysql_error());mysql_select_db('database') or die('Could not select database');$user=$_SESSION['user'];$mode=$_GET['mode'];if($mode=="newmsg"){ echo "<form action='messaging.php?mode=sendmsg' method='post'>"; echo "<div>"; echo "To: <input type='text' name='to' value='' /><br />"; echo "Subject: <input type='text' name='subject' value='' /><br />"; echo "Message:<br />"; echo "<textarea rows='10' cols='50' name='message'></textarea><br />"; echo "<input type='submit' name='submit' value='Send' />"; echo "</div>"; echo "</form>";}elseif($mode=="viewinbox"){ $inbox=mysql_query("SELECT * FROM messages WHERE To='$user'"); echo "<table>"; while($row=mysql_fetch_array($inbox)) { echo "<tr>"; echo "<td>" . $row['To'] . "</td>"; echo "<td><a href=messages?mode=viewmsg&id=" . $row['Id'] . ">" . $row['Subject'] . "</a></td>"; echo "</tr>"; } echo "</table>";}elseif($mode=="viewsent"){ $sent=mysql_query("SELECT * FROM messages WHERE From='$user'"); echo "<table>"; while($row=mysql_fetch_array($sent)) { echo "<tr>"; echo "<td>" . $row['To'] . "</td>"; echo "<td><a href=messages?mode=viewmsg&id=" . $row['Id'] . ">" . $row['Subject'] . "</a></td>"; echo "</tr>"; } echo "</table>";}else{ $inbox=mysql_query("SELECT * FROM messages WHERE To='$user'"); echo "<table>"; while($row=mysql_fetch_array($inbox)) { echo "<tr>"; echo "<td>" . $row['To'] . "</td>"; echo "<td><a href=messages?mode=viewmsg&id=" . $row['Id'] . ">" . $row['Subject'] . "</a></td>"; echo "</tr>"; } echo "</table>";}mysql_close($link);?></div></body></html> There's many issues with this. Each part loads correctly, but the inbox and outbox querys give this error:Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/www/testtools.freehostia.com/messages.phpAnd of course give the line number. Sure enough, it's the array line.Then sending a message doesn't work either. When you click send (I put a mysql_error() to see what the problem was) it says Error: Query was empty. I tried displaying the variables on the same page that displays that error and the variables all show, message and all. So I do not know what the problem there is either. Any ideas? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 12, 2008 Share Posted May 12, 2008 The warning from mysql_fetch_array means that there was a syntax error in the query. You can use the mysql_error function to print the error message. Link to comment Share on other sites More sharing options...
shadowayex Posted May 12, 2008 Author Share Posted May 12, 2008 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/www/testtools.freehostia.com/messages.php on line 107Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'To='shadowayex'' at line 1That's the error I get. shadowayex is the username I'm using to test, so it replaced the To='$user' into To='shadowayex'. But I don't see why it's not right. The line looks like:$inbox=mysql_query("SELECT * FROM messages WHERE To='$user'");Do you see anyting wrong? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 12, 2008 Share Posted May 12, 2008 I bet To is a reserved word. Link to comment Share on other sites More sharing options...
shadowayex Posted May 12, 2008 Author Share Posted May 12, 2008 Ok, I got that fixed. To and From couldn't be used, so I changed to Receiver and Sender. Did you read my problem with the sending form? If so, do you have any idea about that? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 12, 2008 Share Posted May 12, 2008 Well, there's a problem with this line:if(mode==sendmsg) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.