wev Posted March 20, 2012 Share Posted March 20, 2012 hi there..im new to php mysql and im having trouble inserting a string data to mysql from a php date() function. here's my code: $year = date('Y'); echo $year; $insertSQL = sprintf("INSERT INTO tbl_elections (election_id=$year)"); mysql_select_db($database_organizazone_db, $organizazone_db); $Result1 = mysql_query($insertSQL, $organizazone_db) or die(mysql_error()); when i try to output the $year variable on a webpage, it returns "2012" but when i try to insert this data into my database table, it returns an error like this: check the manual that corresponds to your MySQL server version for the right syntax to use near '=2012)' is there a way to convert "2012" into a normal string data type? Link to comment https://forums.phpfreaks.com/topic/259359-insert-a-varchar-data-into-mysql-from-a-php-date-function/ Share on other sites More sharing options...
samshel Posted March 20, 2012 Share Posted March 20, 2012 you don't need to convert number to string to store in varchar. I think your insert statement needs to be updated: try this: INSERT INTO tbl_elections (election_id) values ($year) Link to comment https://forums.phpfreaks.com/topic/259359-insert-a-varchar-data-into-mysql-from-a-php-date-function/#findComment-1329548 Share on other sites More sharing options...
wev Posted March 20, 2012 Author Share Posted March 20, 2012 it said: unexpected T_STRING in C:\xampp\htdocs\ABEOrgzone\elections\election.php i've successfully inserted a data with similar code earlier..but the data it stored on my database was -1 instead of the year 2012 Link to comment https://forums.phpfreaks.com/topic/259359-insert-a-varchar-data-into-mysql-from-a-php-date-function/#findComment-1329562 Share on other sites More sharing options...
samshel Posted March 20, 2012 Share Posted March 20, 2012 $year = date('Y'); echo $year; $insertSQL = "INSERT INTO tbl_elections (election_id) values ($year)"; mysql_select_db($database_organizazone_db, $organizazone_db); $Result1 = mysql_query($insertSQL, $organizazone_db) or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/259359-insert-a-varchar-data-into-mysql-from-a-php-date-function/#findComment-1329563 Share on other sites More sharing options...
wev Posted March 20, 2012 Author Share Posted March 20, 2012 whew... so that's just it?! finally got it right.. thank's a lot!! Link to comment https://forums.phpfreaks.com/topic/259359-insert-a-varchar-data-into-mysql-from-a-php-date-function/#findComment-1329564 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.