rfighter Posted July 13, 2008 Share Posted July 13, 2008 Hi, When I run the following code: if ($_POST[to]) { //the form has been submitted. Now we have to make it secure and insert it into the database $subject = htmlspecialchars(addslashes("$_POST[subject]")); $message = htmlspecialchars(addslashes("$_POST[message]")); $to = htmlspecialchars(addslashes("$_POST[to]")); //the above lines remove html and add \ before all " $getusersql = mysql_query("SELECT * FROM `users` WHERE `username` = '$to'"); $getuserid = mysql_fetch_array($getusersql); $getsendersql = mysql_query("SELECT * FROM `users` WHERE `username` = '$logged[username]'"); $getsenderid = mysql_fetch_array($getsendersql); $ip = $_SERVER['REMOTE_ADDR']; $unread = "unread"; $send = "INSERT INTO pmessages (title, message, touser, from, unread, ip) VALUES ('$subject', '$message', '$getuserid[id]', '$getsenderid[id]', '$unread', '$ip')"; mysql_query($send) or die('Error, insert query failed'); I receive the error "error, insert query failed". I've echoed the query, which gives INSERT INTO pmessages (title, message, touser, from, unread, ip) VALUES ('testt', 'test', '2', '2', 'unread', 'xx.xxx.xx.xx') ...as it should, and I can't see a problem with the syntax. Any help would be appreciated! Thanks. Link to comment https://forums.phpfreaks.com/topic/114571-solved-php-and-mysql-insert-query-failed/ Share on other sites More sharing options...
trq Posted July 13, 2008 Share Posted July 13, 2008 from is a special word in sql, you will either need to change your field name, or surround it with `backticks` within your query. Link to comment https://forums.phpfreaks.com/topic/114571-solved-php-and-mysql-insert-query-failed/#findComment-589119 Share on other sites More sharing options...
rfighter Posted July 13, 2008 Author Share Posted July 13, 2008 It works now, many thanks! Link to comment https://forums.phpfreaks.com/topic/114571-solved-php-and-mysql-insert-query-failed/#findComment-589130 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.