White_Lily Posted November 7, 2012 Share Posted November 7, 2012 Hi, I am trying build a system in which users can send messages to eachother. The problem im having is that the script is having problems inserting the message into the database and i cant figure out why. Here is the code form that the user fills out: <?php echo '<div id="pronav">'; echo '<ul>'; echo '<li><p>Nav to go here.</p></li>'; echo '</ul>'; echo '<div class="clear"></div>'; echo '</div>'; echo '<img src="'.$GLOBALS["nav"].'images/vertical-rule.png" />'; echo '<p>'.$u.'</p>'; echo '<form action="" method="POST">'; echo '<table border="0" style="width:auto;">'; if(!empty($senderr)){ echo '<tr>'; echo '<td colspan="2"><div class="error">Error: '.$senderr.'</div></td>'; echo '</tr>'; }if(!empty($sendpass)){ echo '<tr>'; echo '<td colspan="2"><div class="pass">Success: '.$sendpass.'</div></td>'; echo '</tr>'; } echo '<tr>'; echo '<td>To:</td>'; echo '<td><input type="text" name="sendto" id="sendto" class="input" /></td>'; echo '</tr>'; echo '<tr>'; echo '<td align="top">Subject:</td>'; echo '<td><input type="text" name="sendsubject" class="input" /></td>'; echo '</tr>'; echo '<tr>'; echo '<td>Message:</td>'; echo '<td><textarea name="writemsg" id="sendmsg" class="input" style="height:200px;"></textarea></td>'; echo '</tr>'; echo '<tr>'; echo '<td colspan="2"><input style="float:right;" type="submit" name="sendmsg" class="buttons" value="Send" /></td>'; echo '</tr>'; echo '</table>'; echo '</form>'; ?> Here is the script that inserts the message: //Send Message $sendmsg = $_POST["sendmsg"]; $to = $_POST["sendto"]; $subject = $_POST["sendsubject"]; $message = $_POST["writemsg"]; if(!empty($sendmsg)){ if(empty($to) || empty($message)){ $senderr = "Please fill out the needed fields in order to send a message. (To: & Message:)"; }else{ $date = date('Y/m/d H:i:s', time()); $putmsg = insert("messages", "id, title, content, to, from, date", "1, '$subject', '$message', '$to', '$u', '$date'"); if($putmsg){ $sendpass = "You have successfully sent your friend a message."; }else{ $senderr = "Failed to send message to your friend. Please try again later.<br><br>".mysql_error(); } } } Here is the error I am recieving: Error: Failed to send message to your friend. Please try again later. 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, date) VALUES (1, 'Testing Subject', 'Testing message', 'White_Lily', '' at line 1 Any help would be appreciated. Link to comment https://forums.phpfreaks.com/topic/270414-sending-a-user-a-message/ Share on other sites More sharing options...
gristoi Posted November 7, 2012 Share Posted November 7, 2012 try $putmsg = insert("messages", "`id`,` title`, `content`, `to`, `from`, `date`", "1, '$subject', '$message', '$to', '$u', '$date'"); Link to comment https://forums.phpfreaks.com/topic/270414-sending-a-user-a-message/#findComment-1390803 Share on other sites More sharing options...
White_Lily Posted November 7, 2012 Author Share Posted November 7, 2012 Hmm, okay - that worked thanks! I didn't relize just putting ` around each column name made a difference... Link to comment https://forums.phpfreaks.com/topic/270414-sending-a-user-a-message/#findComment-1390805 Share on other sites More sharing options...
gristoi Posted November 7, 2012 Share Posted November 7, 2012 have a quick look at this. there are certain reserved words in mysql. using the escapes allows you to use them as column names, but this is discouraged. https://dev.mysql.com/doc/refman/5.5/en/reserved-words.html Link to comment https://forums.phpfreaks.com/topic/270414-sending-a-user-a-message/#findComment-1390808 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.