Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/259362-concatenated-php-string-into-mysql/
Share on other sites

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.

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.