Forums / Upgrading to 1.5.x / 1062 : Duplicate entry 'nnnnnn' for key 'PRIMARY' :: insert into sessions

1062 : Duplicate entry 'nnnnnn' for key 'PRIMARY' :: insert into sessions

Views: 1,763

Results 1 to 6 of 6
7 Mar 2015, 1:08 AM
#1
toyseller avatar

toyseller

Zen Follower

Join Date:
Jun 2008
Location:
UK
Posts:
209
Plugin Contributions:
0

1062 : Duplicate entry 'nnnnnn' for key 'PRIMARY' :: insert into sessions

In my log I found this message ...

[04-Mar-2015 17:00:41 Europe/London] PHP Fatal error: 1062:Duplicate entry 'nnnnnn' for key 'PRIMARY' :: insert into sessions

And saw this post ...
http://www.zen-cart.com/showthread.php?215694-Known-Bugs-%28and-fixes%29-with-v1-5-4/page2

I followed the post and did the coding changes ...

And am now getting this message ...
PHP Parse error: syntax error, unexpected 'delete' (T_STRING) in /home/tidytoys/public_html/includes/functions/sessions.php on line 69

This is my current includes functions/sessions.php

Can you please tell me what is wrong.

line 58 - 92

   $val = base64_encode($val);

    global $SESS_LIFE;
    $expiry = time() + $SESS_LIFE;

    $sql = "insert into " . TABLE_SESSIONS . " (sesskey, expiry, `value`)
                values (:zkey, :zexpiry, :zvalue)
                ON DUPLICATE KEY UPDATE `value`=:zvalue, expiry=:zexpiry";
    $sql = $db->bindVars($sql, ':zkey', $key, 'string');
    $sql = $db->bindVars($sql, ':zexpiry', $expiry, 'integer');
    $sql = $db->bindVars($sql, ':zvalue', $val, 'string');
    $result = $db->Execute($sql);
 
    return (!empty($result) && !empty($result->resource));
  }

  function _sess_destroy($key) {
    global $db;
    $sql = "delete from " . TABLE_SESSIONS . " where sesskey = '" . zen_db_input($key) . "'";
    $db->Execute($sql);
    return TRUE;
  }

  function _sess_gc($maxlifetime) {
    global $db;
    $sql = "delete from " . TABLE_SESSIONS . " where expiry < " . time();
    $db->Execute($sql);
    return true;
  }


  // Initialize session save-handler
  session_set_save_handler('_sess_open', '_sess_close', '_sess_read', '_sess_write', '_sess_destroy', '_sess_gc');
  // write and close session at the end of scripts, and before objects are destroyed
  register_shutdown_function('session_write_close');

7 Mar 2015, 3:47 PM
#2
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: 1062 : Duplicate entry 'nnnnnn' for key 'PRIMARY' :: insert into sessions

toyseller:

And am now getting this message ...
PHP Parse error: syntax error, unexpected 'delete' (T_STRING) in /home/tidytoys/public_html/includes/functions/sessions.php on line 69

A "T_STRING" error is caused by an imbalance in the quote or apostrophe characters.

The line number indicated is somewhat misleading in these cases, with the actual error being somewhere in a line of code that precedes the line indicated. The PHP Parser doesn't detect this imbalance until it reaches a part of the code that it determines to be syntaxually incorrect. In your case this is the line containing the 'delete'.

toyseller:

This is my current includes functions/sessions.php
Can you please tell me what is wrong.

line 58 - 92

$val = base64_encode($val);

global $SESS_LIFE;
$expiry = time() + $SESS_LIFE;

$sql = "insert into " . TABLE_SESSIONS . " (sesskey, expiry, `value`)
            values (:zkey, :zexpiry, :zvalue)
            ON DUPLICATE KEY UPDATE `value`=:zvalue, expiry=:zexpiry";
$sql = $db->bindVars($sql, ':zkey', $key, 'string');
$sql = $db->bindVars($sql, ':zexpiry', $expiry, 'integer');
$sql = $db->bindVars($sql, ':zvalue', $val, 'string');
$result = $db->Execute($sql);

return (!empty($result) && !empty($result->resource));

}

function _sess_destroy($key) {
global $db;
$sql = "delete from " . TABLE_SESSIONS . " where sesskey = '" . zen_db_input($key) . "'";
$db->Execute($sql);
return TRUE;
}

function _sess_gc($maxlifetime) {
global $db;
$sql = "delete from " . TABLE_SESSIONS . " where expiry < " . time();
$db->Execute($sql);
return true;
}

// Initialize session save-handler
session_set_save_handler('_sess_open', '_sess_close', '_sess_read', '_sess_write', '_sess_destroy', '_sess_gc');
// write and close session at the end of scripts, and before objects are destroyed
register_shutdown_function('session_write_close');


I'm not spotting anything amiss in the code as posted (other than an unbalanced '}' and that is because the code you have given starts partway within the    _sess_read($key) function.  

The session.php code is pretty small, so it will probably be worthwhile to provide a copy of the entire file as that will give us a better chance to see exactly where the error is.  

Cheers
RodG

NOTE: We are sorry that Rod is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

Ozpost - The Ultimate Shipping module for Australian Merchants. Click these links for its Homepage, or Download from Zen-Cart.com.

7 Mar 2015, 4:12 PM
#3
toyseller avatar

toyseller

Zen Follower

Join Date:
Jun 2008
Location:
UK
Posts:
209
Plugin Contributions:
0

Re: 1062 : Duplicate entry 'nnnnnn' for key 'PRIMARY' :: insert into sessions

Thank you RodG :-D

I only changed that portion of code.
Line 69 is this line ... $result = $db->Execute($sql);

<?php
/**
 * functions/sessions.php
 * Session functions
 *
 * @package functions
 * @copyright Copyright 2003-2014 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version GIT: $Id: Author: DrByte  Wed Mar 12 22:37:22 2014 -0400 Modified in v1.5.3 $
 */
if (!defined('IS_ADMIN_FLAG')) {
  die('Illegal Access');
}
  if (IS_ADMIN_FLAG === true) {
    if (PADSS_ADMIN_SESSION_TIMEOUT_ENFORCED != 0 && SESSION_TIMEOUT_ADMIN > 900) {
      $SESS_LIFE = 900;
    } else {
      $SESS_LIFE = (int)SESSION_TIMEOUT_ADMIN;
    }
  } else {
    if (defined('SESSION_TIMEOUT_CATALOG') && (int)SESSION_TIMEOUT_CATALOG > 120) {
      $SESS_LIFE = (int)SESSION_TIMEOUT_CATALOG;
    } else
    if (!$SESS_LIFE = get_cfg_var('session.gc_maxlifetime')) {
      $SESS_LIFE = 1440;
    }
  }

  function _sess_open($save_path, $session_name) {
    return true;
  }

  function _sess_close() {
    return true;
  }

  function _sess_read($key) {
    global $db;
    $qid = "select value
            from " . TABLE_SESSIONS . "
            where sesskey = '" . zen_db_input($key) . "'
            and expiry > '" . time() . "'";

    $value = $db->Execute($qid);

    if (isset($value->fields['value']) && $value->fields['value']) {
      $value->fields['value'] = base64_decode($value->fields['value']);
      return $value->fields['value'];
    }

    return ("");
  }

  function _sess_write($key, $val) {
    global $db;
    if (!is_object($db)) return;
    $val = base64_encode($val);

    global $SESS_LIFE;
    $expiry = time() + $SESS_LIFE;

    $sql = "insert into " . TABLE_SESSIONS . " (sesskey, expiry, `value`)
                values (:zkey, :zexpiry, :zvalue)
                ON DUPLICATE KEY UPDATE `value`=:zvalue, expiry=:zexpiry";
    $sql = $db->bindVars($sql, ':zkey', $key, 'string');
    $sql = $db->bindVars($sql, ':zexpiry', $expiry, 'integer');
    $sql = $db->bindVars($sql, ':zvalue', $val, 'string');
    $result = $db->Execute($sql);
 
    return (!empty($result) && !empty($result->resource));
  }

  function _sess_destroy($key) {
    global $db;
    $sql = "delete from " . TABLE_SESSIONS . " where sesskey = '" . zen_db_input($key) . "'";
    $db->Execute($sql);
    return TRUE;
  }

  function _sess_gc($maxlifetime) {
    global $db;
    $sql = "delete from " . TABLE_SESSIONS . " where expiry < " . time();
    $db->Execute($sql);
    return true;
  }


  // Initialize session save-handler
  session_set_save_handler('_sess_open', '_sess_close', '_sess_read', '_sess_write', '_sess_destroy', '_sess_gc');
  // write and close session at the end of scripts, and before objects are destroyed
  register_shutdown_function('session_write_close');


  function zen_session_start() {
    @ini_set('session.gc_probability', 1);
    @ini_set('session.gc_divisor', 2);
    if (IS_ADMIN_FLAG === true) {
      @ini_set('session.gc_maxlifetime', (SESSION_TIMEOUT_ADMIN > 900 ? 900 : SESSION_TIMEOUT_ADMIN));
    } elseif (defined('SESSION_TIMEOUT_CATALOG') && (int)SESSION_TIMEOUT_CATALOG > 120) {
      @ini_set('session.gc_maxlifetime', (int)SESSION_TIMEOUT_CATALOG);
    }
    if (preg_replace('/[a-zA-Z0-9,-]/', '', session_id()) != '')
    {
      zen_session_id(md5(uniqid(rand(), true)));
    }
    $temp = session_start();
    if (!isset($_SESSION['securityToken'])) {
      $_SESSION['securityToken'] = md5(uniqid(rand(), true));
    }
    return $temp;
  }

  function zen_session_id($sessid = '') {
    if (!empty($sessid)) {
      $tempSessid = $sessid;
      if (preg_replace('/[a-zA-Z0-9,-]/', '', $tempSessid) != '')
      {
        $sessid = md5(uniqid(rand(), true));
      }
      return session_id($sessid);
    } else {
      return session_id();
    }
  }

  function zen_session_name($name = '') {
    if (!empty($name)) {
      $tempName = $name;
      if (preg_replace('/[a-zA-Z0-9,-]/', '', $tempName) == '') return session_name($name);
      return FALSE;
    } else {
      return session_name();
    }
  }

  function zen_session_write_close() {
    return session_write_close();
  }

  function zen_session_destroy() {
    return session_destroy();
  }

  function zen_session_save_path($path = '') {
    if (!empty($path)) {
      return session_save_path($path);
    } else {
      return session_save_path();
    }
  }

  function zen_session_recreate() {
    global $http_domain, $https_domain, $current_domain;
      if ($http_domain == $https_domain) {
      $saveSession = $_SESSION;
      $oldSessID = session_id();
      session_regenerate_id();
      $newSessID = session_id();
      session_id($oldSessID);
      session_id($newSessID);
      session_set_save_handler('_sess_open', '_sess_close', '_sess_read', '_sess_write', '_sess_destroy', '_sess_gc');
      $_SESSION = $saveSession;
      if (IS_ADMIN_FLAG !== true) {
        whos_online_session_recreate($oldSessID, $newSessID);
      }
    }
  }

7 Mar 2015, 4:54 PM
#4
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: 1062 : Duplicate entry 'nnnnnn' for key 'PRIMARY' :: insert into sessions

toyseller:

Thank you RodG :-D
I only changed that portion of code.
Line 69 is this line ... $result = $db->Execute($sql);

Shoot. This is a worry. There are no syntax errors in this code.

I don't know if it will provide any further clues or not, but I would suggest you try inserting a line that reads

echo $sql ; die ;

immediately before the line reading $result = $db->Execute($sql);

I will warn you that the results won't be pretty (don't do this on a live site) and it will effectively 'kill' the store when this part of the code is reached. Anyway, if you do this and post a copy of the output you see it might provide a bit more insight as to the cause. (Although I'm really not confident about this).

Theory dictates that you really shouldn't be getting the error that you are reporting and we are barking up the wrong tree.

Another thing I would suggest would be to restore this file back to an original copy so as to confirm verify that the problem really is caused by the changes made to this file. In fact this is probably the 1st thing to try, especially if the site is live.

Other than this, I'm all out of ideas at the moment.

Cheers
RodG

NOTE: We are sorry that Rod is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

Ozpost - The Ultimate Shipping module for Australian Merchants. Click these links for its Homepage, or Download from Zen-Cart.com.

7 Mar 2015, 6:45 PM
#5
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: 1062 : Duplicate entry 'nnnnnn' for key 'PRIMARY' :: insert into sessions

How were the changes made to the file? (Ie. what editor was used?) (Trying to think of other reasons, as the code to the eyes looks right...)

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

7 Mar 2015, 6:56 PM
#6
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,119
Plugin Contributions:
56

Re: 1062 : Duplicate entry 'nnnnnn' for key 'PRIMARY' :: insert into sessions

Could the error possibly be corrected by changing

<?php
/**
 * functions/sessions.php
 * Session functions
 *
 * @package functions
 * @copyright Copyright 2003-2014 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version GIT: $Id: Author: DrByte  Wed Mar 12 22:37:22 2014 -0400 Modified in v1.5.3 $
 */
if (!defined('IS_ADMIN_FLAG')) {
  die('Illegal Access');
}
  if (IS_ADMIN_FLAG === true) {
    if (PADSS_ADMIN_SESSION_TIMEOUT_ENFORCED != 0 && SESSION_TIMEOUT_ADMIN > 900) {
      $SESS_LIFE = 900;
    } else {
      $SESS_LIFE = (int)SESSION_TIMEOUT_ADMIN;
    }
  } else {
    if (defined('SESSION_TIMEOUT_CATALOG') && (int)SESSION_TIMEOUT_CATALOG > 120) {
      $SESS_LIFE = (int)SESSION_TIMEOUT_CATALOG;
    } else
    if (!$SESS_LIFE = get_cfg_var('session.gc_maxlifetime')) {
      $SESS_LIFE = 1440;
    }
  }

to

<?php
/**
 * functions/sessions.php
 * Session functions
 *
 * @package functions
 * @copyright Copyright 2003-2014 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version GIT: $Id: Author: DrByte  Wed Mar 12 22:37:22 2014 -0400 Modified in v1.5.3 $
 */
if (!defined('IS_ADMIN_FLAG')) {
  die('Illegal Access');
}
  if (IS_ADMIN_FLAG === true) {
    if (PADSS_ADMIN_SESSION_TIMEOUT_ENFORCED != 0 && SESSION_TIMEOUT_ADMIN > 900) {
      $SESS_LIFE = 900;
    } else {
      $SESS_LIFE = (int)SESSION_TIMEOUT_ADMIN;
    }
  } else {
    if (defined('SESSION_TIMEOUT_CATALOG') && (int)SESSION_TIMEOUT_CATALOG > 120) {
      $SESS_LIFE = (int)SESSION_TIMEOUT_CATALOG;
    } else [B]{[/B]
    if (!$SESS_LIFE = get_cfg_var('session.gc_maxlifetime')) {
      $SESS_LIFE = 1440;
    }
  }