Fredish Posted August 12, 2008 Share Posted August 12, 2008 I recently ran into a problem I've never seen before. I try to write a row into my table (ABCmeetings), but though the script is flawless it won't write the values to the database. So I added "or die(mysql_error());" to the line to see what was wrong and the outcome was this: 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 's','vf','normal','2008-08-12 22:40:43','Fred Miller' )' at line 1 What the hell? I thought. I have no idea of how to fix this. Please help me! Link to comment https://forums.phpfreaks.com/topic/119380-you-have-an-error-in-your-sql-syntax/ Share on other sites More sharing options...
Jabop Posted August 12, 2008 Share Posted August 12, 2008 Apparently your script isn't flawless. Show us the whole query. Link to comment https://forums.phpfreaks.com/topic/119380-you-have-an-error-in-your-sql-syntax/#findComment-614986 Share on other sites More sharing options...
Fredish Posted August 12, 2008 Author Share Posted August 12, 2008 mysql_query( "INSERT INTO ABCmeetings ( location, say, tone, date, name ) VALUES( '$bossloc','$say','$tone','$date','$boss' )" ) or die(mysql_error()); That's the query. I've checked the variables and they all check out. I am pretty sure there's nothing wrong with the code. I've connected to the database and everything, I'm sure I haven't misspelled my database name. Link to comment https://forums.phpfreaks.com/topic/119380-you-have-an-error-in-your-sql-syntax/#findComment-614987 Share on other sites More sharing options...
DarkWater Posted August 12, 2008 Share Posted August 12, 2008 Date is a MySQL reserved keyword, so you can't have a column named after it. Link to comment https://forums.phpfreaks.com/topic/119380-you-have-an-error-in-your-sql-syntax/#findComment-614990 Share on other sites More sharing options...
Fredish Posted August 12, 2008 Author Share Posted August 12, 2008 I have used date as a column name in the past and it worked just fine. Though I tried changing it to: "indate" but the outcome was the same. Link to comment https://forums.phpfreaks.com/topic/119380-you-have-an-error-in-your-sql-syntax/#findComment-614992 Share on other sites More sharing options...
revraz Posted August 12, 2008 Share Posted August 12, 2008 Actually Date is fine. I'm wondering if its the missing space after VALUES and before ( because it looks fine. Link to comment https://forums.phpfreaks.com/topic/119380-you-have-an-error-in-your-sql-syntax/#findComment-614994 Share on other sites More sharing options...
revraz Posted August 12, 2008 Share Posted August 12, 2008 Does this work mysql_query( "INSERT INTO ABCmeetings (location, say, tone, date, name) VALUES ('s','vf','normal','2008-08-12 22:40:43','Fred Miller')" ) or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/119380-you-have-an-error-in-your-sql-syntax/#findComment-614995 Share on other sites More sharing options...
Fredish Posted August 12, 2008 Author Share Posted August 12, 2008 I tried adding the space so it looked like this: mysql_query( "INSERT INTO ABCmeetings ( location, say, tone, indate, name ) VALUES ( '$bossloc','$say','$tone','$date','$boss' )" ) or die(mysql_error()); But the outcome was still the same. Link to comment https://forums.phpfreaks.com/topic/119380-you-have-an-error-in-your-sql-syntax/#findComment-614997 Share on other sites More sharing options...
Fredish Posted August 12, 2008 Author Share Posted August 12, 2008 Does this work mysql_query( "INSERT INTO ABCmeetings (location, say, tone, date, name) VALUES ('s','vf','normal','2008-08-12 22:40:43','Fred Miller')" ) or die(mysql_error()); Yes it did! so maybe it's the variables... Here they are: $say = $_POST[ 'say' ]; $gang = $_COOKIE[ 'gang' ]; $tone = "normal"; $locations = mysql_query("SELECT * FROM ABCgang WHERE gang='$gang' AND rank='Boss'"); $location = mysql_fetch_array( $locations ); $boss = $location['name']; $bossloc = $location['location']; $date = date('Y-m-d H:i:s'); Link to comment https://forums.phpfreaks.com/topic/119380-you-have-an-error-in-your-sql-syntax/#findComment-614998 Share on other sites More sharing options...
revraz Posted August 12, 2008 Share Posted August 12, 2008 echo $bossloc and see if there is a extra ' in there. Link to comment https://forums.phpfreaks.com/topic/119380-you-have-an-error-in-your-sql-syntax/#findComment-614999 Share on other sites More sharing options...
Fredish Posted August 12, 2008 Author Share Posted August 12, 2008 echo $location and see if there is a extra ' in there. Omg you are totally right! $location is "Jonesey's"! that's some fancy work there revraz! I didn't know you couldn't have ':s in databases. How can I fix that? Link to comment https://forums.phpfreaks.com/topic/119380-you-have-an-error-in-your-sql-syntax/#findComment-615001 Share on other sites More sharing options...
revraz Posted August 12, 2008 Share Posted August 12, 2008 Need to escape them. Link to comment https://forums.phpfreaks.com/topic/119380-you-have-an-error-in-your-sql-syntax/#findComment-615006 Share on other sites More sharing options...
Fredish Posted August 12, 2008 Author Share Posted August 12, 2008 Not familiar at all with that, but I did this: $bossloc = $location['location']; $bossloc = array_map( "mysql_real_escape_string", $bossloc ); And the values of $bossloc is nothing. Did I do something wrong? I'm sorry for being so darn newbie Link to comment https://forums.phpfreaks.com/topic/119380-you-have-an-error-in-your-sql-syntax/#findComment-615008 Share on other sites More sharing options...
revraz Posted August 12, 2008 Share Posted August 12, 2008 When you store the value, you can use addslashes and when you retrieve it, you can use stripslashes. Link to comment https://forums.phpfreaks.com/topic/119380-you-have-an-error-in-your-sql-syntax/#findComment-615012 Share on other sites More sharing options...
Fredish Posted August 12, 2008 Author Share Posted August 12, 2008 It worked like a charm! Thanks very much dude! Link to comment https://forums.phpfreaks.com/topic/119380-you-have-an-error-in-your-sql-syntax/#findComment-615018 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.