wev Posted March 20, 2012 Share Posted March 20, 2012 here i am again... so i have this algorithm that computes the current academic year for our school.. it outputs the correct academic year on a browser.. but when i try to insert it into my database, the data inserted goes wrong.. it just keeps on inserting -1 instead of the string 2011-2012 here's my code: <?php $year = date('Y'); $currentyear = $year; $lastyear = $year - 1; $nextyear = $year + 1; if (date('m') < 6) { $current_ay = $lastyear."-".$currentyear; } else if (date('m') >= 6) { $current_ay = $currentyear."-".$nextyear; } echo $current_ay; $insertSQL = "INSERT INTO tbl_elections (election_id) values ($current_ay)"; mysql_select_db($database_organizazone_db, $organizazone_db); $Result1 = mysql_query($insertSQL, $organizazone_db) or die(mysql_error()); ?> *the database column to be inserted into is of varchar data type Quote Link to comment https://forums.phpfreaks.com/topic/259362-concatenated-php-string-into-mysql/ Share on other sites More sharing options...
DavidAM Posted March 20, 2012 Share Posted March 20, 2012 Is the column for the current academic year actually called "election_id" or do you have the wrong column name in that INSERT statement? Quote Link to comment https://forums.phpfreaks.com/topic/259362-concatenated-php-string-into-mysql/#findComment-1329577 Share on other sites More sharing options...
Muddy_Funster Posted March 20, 2012 Share Posted March 20, 2012 a classic example of missing quotes for string input. you are telling the DB that you want to enter 2011 - 2012 it's taking that as an arithmetic calculation and entering the result. wrap quotes around your variable name and your all good. Quote Link to comment https://forums.phpfreaks.com/topic/259362-concatenated-php-string-into-mysql/#findComment-1329579 Share on other sites More sharing options...
wev Posted March 20, 2012 Author Share Posted March 20, 2012 @david.. yes. my column name is correct.. @muddy_funster.. i did wrap it in quotes and it inserted a data into my database..but it only stored "2011". it did not include the "-2012" --almost there! Quote Link to comment https://forums.phpfreaks.com/topic/259362-concatenated-php-string-into-mysql/#findComment-1329584 Share on other sites More sharing options...
wev Posted March 20, 2012 Author Share Posted March 20, 2012 wrapping my variable with quotes actually inserted two(2) new rows to my database..one with zero "0" value,and another with "2011" value Quote Link to comment https://forums.phpfreaks.com/topic/259362-concatenated-php-string-into-mysql/#findComment-1329587 Share on other sites More sharing options...
Muddy_Funster Posted March 20, 2012 Share Posted March 20, 2012 like this: $insertSQL = "INSERT INTO tbl_elections (election_id) values ('$current_ay')"; Quote Link to comment https://forums.phpfreaks.com/topic/259362-concatenated-php-string-into-mysql/#findComment-1329588 Share on other sites More sharing options...
wev Posted March 20, 2012 Author Share Posted March 20, 2012 yeah..that's what i did.. figured out that the problem was with the datatype of the election_id column on my database..it was on int when it was supposed to be a varchar.. got it working now..thank you so much for your help.. Quote Link to comment https://forums.phpfreaks.com/topic/259362-concatenated-php-string-into-mysql/#findComment-1329592 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.