Jump to content

insert a varchar data into mysql from a php date() function


wev

Recommended Posts

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
Share on other sites

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
Share on other sites

$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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.