Stalingrad Posted April 7, 2008 Share Posted April 7, 2008 Hey. =] I've been having a lot of trouble with PHP and MYSQL lately; (even though I've been programming in hboth for teh past three years). This time, though; I'm completely stumped. Here's what's going on: I'm trying to create a Personal Messaging System on my Website, and for teh "Compose", the information that I insert into the form will not enter into the database. My connection is fine, because I was able to register an account on my site. I've tied having MySQL display the error, but it makes no sense what-so-ever. Here's the Message script; (I'll put the query that won't insert the data in bold): <?php session_start(); include("config.php"); checkit(); start(); $title = "<font size=6>Inbox</font>"; $action = $_GET['action']; $messages = mysql_query("SELECT * FROM messages WHERE to='$username' ORDER BY id"); while($inbox = @mysql_fetch_array($messages)) { $messageid = $inbox['id']; $for = $inbox['to']; $sender = $inbox['from']; $datesent = $inbox['date']; $msubject = $inbox['subject']; $mbody = $inbox['body']; $mstatus = $inbox['status']; }if($action != "compose" && $action != "view" && $action != "delete" && $action != "reply") { echo "$title<br><br><a href=?action=compose>Compose</a><br><br><table border=\"1\" bordercolor=\"black\" cellpadding=\"10\" cellspacing=\"0\" height=\"24px\" width=\"100%\"><tr><td><b><center>From</center></b></td><td><b><center>Subject</center></b></td></tr><tr><td>$sender</td><td>$msubject</td></tr></table>"; } if($action == "compose") { $ssubject = $_POST['subject']; $sto = $_POST['to']; $sbody = $_POST['body']; $send = $_POST['send']; if(!isset($send)) { echo "<font size=6>Compose</font><br><br><a href=?action=compose>Compose</a><br><br><table border=\"1\" bordercolor=\"black\" cellpadding=\"5\" cellspacing=\"0\" height=\"100%\" width=\"440px\"><tr><td>"; ?><html><form action="<?php echo "$PHP_SELF"; ?>" method="POST">Subject: <input type="text" name="subject"> To: <input type="text" name="to"></td></tr><tr><td><textarea name="body" cols="50" rows="14"></textarea></td></tr></table><br><br><input type="submit" name="send" value="Send Message"></form></html><?php } if(isset($send)) { if($ssubject == "" || $sto == "" || $sbody == "" ) { echo "<font size=6>Inbox - Compose</font><br><br><a href=?action=compose>Compose</a><br><br><font color=red>Error! Please Fill out the Entire Form.</font><br><br><table border=\"1\" bordercolor=\"black\" cellpadding=\"5\" cellspacing=\"0\" height=\"100%\" width=\"440px\"><tr><td>"; ?><html><form action="<?php echo "$PHP_SELF"; ?>" method="POST">Subject: <input type="text" name="subject"> To: <input type="text" name="to"></td></tr><tr><td><textarea name="body" cols="50" rows="14"></textarea></td></tr></table><br><br><input type="submit" name="send" value="Send Message"></form></html><?php } $checkto = mysql_num_rows(mysql_query("SELECT id FROM users WHERE username='$sto'")); if($checkto == "0") { echo "<font size=6>Inbox - Compose</font><br><br><a href=?action=compose>Compose</a><br><br><font color=red>Error! That User doesn't Exist.</font><br><br><table border=\"1\" bordercolor=\"black\" cellpadding=\"5\" cellspacing=\"0\" height=\"100%\" width=\"440px\"><tr><td>"; ?><html><form action="<?php echo "$PHP_SELF"; ?>" method="POST">Subject: <input type="text" name="subject"> To: <input type="text" name="to"></td></tr><tr><td><textarea name="body" cols="50" rows="14"></textarea></td></tr></table><br><br><input type="submit" name="send" value="Send Message"></form></html><?php } if($ssubject != "" && $sto != "" && $checkto >= "1" && $sbody != "") { mysql_query("INSERT INTO messages (to, from, subject, body, status) VALUES ('$sto', '$username', '$ssubject', '$sbody')"); echo "<font size=6>Inbox - Compose</font><br><br><a href=inbox.php>Inbox</a> | <a href=?action=compose>Compose</a><br><br><font color=green>Success! Your Message has been Sent.</font><br>"; } } stop(); } ?> Also; to make it a bit more easier on everybody, I'll also post the MySQL Error that I get when I submit a message to myself: 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, from, subject, body, status) VALUES ('Stalingrad', 'Stalingrad', 'Subject', ' at line 1 Does anybody know what could be wrong? Thanks in advance! =D Link to comment https://forums.phpfreaks.com/topic/99940-data-wont-insert/ Share on other sites More sharing options...
GingerRobot Posted April 7, 2008 Share Posted April 7, 2008 To and From are reserved words in mysql (see: http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html) Either rename your fields (better in the long run) or enclose them in backticks (`). Link to comment https://forums.phpfreaks.com/topic/99940-data-wont-insert/#findComment-511040 Share on other sites More sharing options...
Stalingrad Posted April 7, 2008 Author Share Posted April 7, 2008 Wow, thank you VERY much! =] Link to comment https://forums.phpfreaks.com/topic/99940-data-wont-insert/#findComment-511046 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.