lockdownd7 Posted September 3, 2009 Share Posted September 3, 2009 I'm trying to write string variables into a table. Something like this: $string = "random string"; $anotherstring = "random string"; mysql_query("INSERT INTO table (field1, field2) VALUES("$string", "$anotherstring") ") or die(mysql_error()); What am I missing? When I run the code, I get an error about MySQL syntax being incorrect. Quote Link to comment https://forums.phpfreaks.com/topic/172911-solved-how-to-send-variable-to-mysql/ Share on other sites More sharing options...
MatthewJ Posted September 3, 2009 Share Posted September 3, 2009 You can't put double quotes inside double quotes without escaping them... or use single quotes like below <?php $string = "random string"; $anotherstring = "random string"; mysql_query("INSERT INTO table (field1, field2) VALUES('$string', '$anotherstring') ") or die(mysql_error()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/172911-solved-how-to-send-variable-to-mysql/#findComment-911300 Share on other sites More sharing options...
lockdownd7 Posted September 3, 2009 Author Share Posted September 3, 2009 Yes, I initially used single quotes, but either way I still get an error. I should probably mention that these strings are unrelated to the database... i.e., I didn't use any mysql query to retrieve them. Quote Link to comment https://forums.phpfreaks.com/topic/172911-solved-how-to-send-variable-to-mysql/#findComment-911304 Share on other sites More sharing options...
MatthewJ Posted September 3, 2009 Share Posted September 3, 2009 Well, the example I posted back inserts fine for me... so, are you sure the table name is correct.. and the field names are correct? Quote Link to comment https://forums.phpfreaks.com/topic/172911-solved-how-to-send-variable-to-mysql/#findComment-911309 Share on other sites More sharing options...
jamesxg1 Posted September 3, 2009 Share Posted September 3, 2009 Hello Mate, mysql_query("INSERT INTO `table` (field1, field2) VALUES('$string', '$anotherstring')") or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR); That will work. May i ask where are you connecting to mysql ?, And what are the errors you get ?, James. Quote Link to comment https://forums.phpfreaks.com/topic/172911-solved-how-to-send-variable-to-mysql/#findComment-911311 Share on other sites More sharing options...
PFMaBiSmAd Posted September 3, 2009 Share Posted September 3, 2009 Posting the actual error message would allow someone to directly help with the problem. Quote Link to comment https://forums.phpfreaks.com/topic/172911-solved-how-to-send-variable-to-mysql/#findComment-911312 Share on other sites More sharing options...
jamesxg1 Posted September 3, 2009 Share Posted September 3, 2009 Here mate this should provide you with all the help you need. http://www.tizag.com/mysqlTutorial/mysqlinsert.php James. Quote Link to comment https://forums.phpfreaks.com/topic/172911-solved-how-to-send-variable-to-mysql/#findComment-911313 Share on other sites More sharing options...
lockdownd7 Posted September 3, 2009 Author Share Posted September 3, 2009 Here's what the relevant section of code looks like: mysql_connect($hostname,$username,$password) or die(mysql_error()); mysql_select_db($database) or die(mysql_error()); $file = date("m.d.y") . ".txt"; if (file_exists($file)) { $fh = fopen($file, 'r'); $string = fread($fh, filesize($file)); fclose($fh); mysql_query("INSERT INTO table (field1) VALUES('$string') ") or die(mysql_error()); } James, I tried your code and still got the following error: Fatal error: Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use Quote Link to comment https://forums.phpfreaks.com/topic/172911-solved-how-to-send-variable-to-mysql/#findComment-911320 Share on other sites More sharing options...
PFMaBiSmAd Posted September 3, 2009 Share Posted September 3, 2009 That's not the complete error message, making it a little hard to help. I'll guess that your data contains SQL special characters that is breaking the query. You need to use mysql_real_escape_string() on all string data put into a query to escape the data. Quote Link to comment https://forums.phpfreaks.com/topic/172911-solved-how-to-send-variable-to-mysql/#findComment-911322 Share on other sites More sharing options...
jamesxg1 Posted September 3, 2009 Share Posted September 3, 2009 Hiya mate, Thats not the full error, So here have a look here. http://www.tizag.com/mysqlTutorial/ James. Quote Link to comment https://forums.phpfreaks.com/topic/172911-solved-how-to-send-variable-to-mysql/#findComment-911323 Share on other sites More sharing options...
lockdownd7 Posted September 3, 2009 Author Share Posted September 3, 2009 My apologies. Here is the full error: Fatal error: Query failed: 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 't you always thought about hitting upon a hot page about [Keyword]? Are you into' at line 1 in C:\xampp\htdocs\test2.php on line 32 Quote Link to comment https://forums.phpfreaks.com/topic/172911-solved-how-to-send-variable-to-mysql/#findComment-911326 Share on other sites More sharing options...
PFMaBiSmAd Posted September 3, 2009 Share Posted September 3, 2009 Reread this - .. your data contains SQL special characters that is breaking the query. You need to use mysql_real_escape_string() on all string data put into a query to escape the data. Quote Link to comment https://forums.phpfreaks.com/topic/172911-solved-how-to-send-variable-to-mysql/#findComment-911327 Share on other sites More sharing options...
lockdownd7 Posted September 3, 2009 Author Share Posted September 3, 2009 Reread this - .. your data contains SQL special characters that is breaking the query. You need to use mysql_real_escape_string() on all string data put into a query to escape the data. Yep, that was it. Just shows how little I know about SQL. Quote Link to comment https://forums.phpfreaks.com/topic/172911-solved-how-to-send-variable-to-mysql/#findComment-911328 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.