Correct MySQL syntax for inserting a variable into expiresDate field

6 posts by 2 authors in: Forums > CMS Builder
Last Post: April 20, 2015   (RSS)

Hi All,

My Client wants to be able to determine their member’s initial subscription date based on a series of calculation, and insert the resulting date into the expiresDate field into their client’s account record when the member signs up.

With some help from Claire Ryan, I was able to calculate what seems to be a valid MySQL date string value of yyyy-mm-dd 00:00:00 in the variable  $adjustedMembershipExpiryDate, IE: 2015-07-31 00:00:00 , but I can’t seem to get that value correctly  inserted into the expiresDate column of the mysql_query("INSERT INTO `{$TABLE_PREFIX}accounts` SET list in my user-signup form.

I was using this up to now, (which worked): expiresDate     = (NOW() + INTERVAL 1 YEAR),

But when I tried to replace that with: expiresDate     = $adjustedMembershipExpiryDate,

It throws a MySQL syntax error, and I'm hoping that a more experienced coder can shed some light on a solution to the issue.

Thanks,
Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Hey Jerry, 

Would it be possible to post the error that 's getting returned, and the block of code that inserting the date? It might be that you need to wrap the expiry date variable with a couple of  commas:

expiresDate     = '$adjustedMembershipExpiryDate',

As when a date is inserted, MySQL treats it as a string.

Thanks,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gkornbluth - April 16, 2015 - edited: April 16, 2015

Sure Greg,

Great suggestion, and it almost works. (see below)

The code that sets the value of the$adjustedMembershipExpiryDate variable is:

<?php // code to check and adjust expires date 4-3-15 ?>
<?php if ( $organization_informationRecord['valid_for_one_year_from_date_joined'] = 0 || $organization_informationRecord['valid_for_one_year_from_date_joined'] = '' ):?>
<?php $curr_month = date("n") ?>
<?php $start_month = $organization_informationRecord['start_month'] ?>
<?php $cutoff_month = $organization_informationRecord['cutoff_month'] ?>
<?php $renewal_duration = $organization_informationRecord['renewal_duration'] ?>
<?php
  $currentYear = date('Y');
  if($curr_month >= $cutoff_month) {
    $renewalYear = $currentYear + ($renewal_duration);
  }
  else {
    $renewalYear = $currentYear + ($renewal_duration - 1);
  }
 
  $expiryMonth = $start_month-1;
  if($expiryMonth < 10) {
    $expiryMonth = "0".$expiryMonth;
  }
  if($expiryMonth == 00) {
    $expiryMonth = 12;
  }
 
  $testEndDate = $renewalYear. '-'.$expiryMonth.'-01';
  $lastDayofMonth = date('t', $testEndDate);
   if($expiryMonth == 02) {
    $lastDayofMonth = 28;
  }
  if($expiryMonth == 04 || $expiryMonth == 06 || $expiryMonth == 08 || $expiryMonth == 11) {
    $lastDayofMonth = 30;
}
  $adjustedMembershipExpiryDate = $renewalYear.'-'.$expiryMonth.'-'.$lastDayofMonth.' 00:00:00';  
  ?>

The variable created (for example) is in the format: 2016-07-31 00:00:00

The code that is inserting the date is:

 mysql_query("INSERT INTO `{$TABLE_PREFIX}accounts` SET
                      first_name         = '".mysql_escape( $_REQUEST['first_name'] )."',
                      middle_initial         = '".mysql_escape( $_REQUEST['middle_initial'] )."',
                      last_name         = '".mysql_escape( $_REQUEST['last_name'] )."',
                      expiresDate      = $adjustedMembershipExpiryDate,
                      neverExpires     = '0',
                      createdDate      = NOW(),
                      updatedDate      = NOW(),
                      createdByUserNum = '0',
                      updatedByUserNum = '0'")
      or die("MySQL Error Creating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
      $userNum = mysql_insert_id();

Without the apostrophes, the error thrown is:

MySQL Error Creating Record:
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 ' neverExpires = '0', createdDate = NOW(), ' at line 51

*************************

With the apostrophes, a user record with the correct expiresDate is created, but the following error is thrown (which is in the code that Clair put together recently):

E_NOTICE: A non well formed numeric value encountered
/home2/public_html/serverpath/apptest.php (line 51)
http://www.thedomain.com/apptest.php

I believe that you have the login credentials for this account if you need it (it's the same account as the captcha issue).

the file that is producing this issue is apptest.php

Thanks for checking into this.

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Hi Greg,

Just an FYI update.

The line of code in the calculation code (above), that Claire helped create:

$lastDayofMonth = date('t', $testEndDate);


Seems to be the cause of the error:
E_NOTICE: A non well formed numeric value encountered
/home2/public_html/serverpath/apptest.php (line 51)
http://www.thedomain.com/apptest.php

When that error causes a miscalculation, resulting in an invalid $adjustedMembershipExpiryDate variable, like 2016-9-31 00:00:00, (there are only 30 days in September), the expiresDate field in the created user record is left blank, even with the apostrophes.

Hope that explains.

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Hey Jerry,

I've taken a look at the code, but I wasn't able to recreate the issue on the site. Is there something in particular I need to enter to put into the fields to get the issue to appear?

While I was looking through the code I created apptest2.php, it might be worth testing with that to see if the issue still appears on that page. 

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com