1
votes

I'm trying to create a login function while learning CodeIgniter. I've validated that both the username and password are correct. The passwords are encrypted using Phppass codeigniter password library.

This is the error I get after I login with the correct information.

A Database Error Occurred

Error Number: 1064

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

'(`session_id`, `ip_address`, `user_agent`, `last_activity`)
 VALUES ('826f937fed9' at line 1

 INSERT INTO (`session_id`, `ip_address`, `user_agent`,
 `last_activity`) VALUES ('826f937fed9185e2c121ec06b1b44b6a', '::1',
 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/53', 1442454002)

Filename: libraries/Session.php

Line Number: 327

2
INSERT ... [INTO] tbl_name ... It's staring you right in the face. - Jared Farrish
I think the problem can be of " ' ". Replace "` session_id `" with " 'session' " and try. i.e. with quotes - ankur140290

2 Answers

0
votes

As Jarred Farrish and Darack on the PHP chatrooms pointed out the error was right in front of me! I had forgotten to add my CodeIgniter database tbl name to the config! Well, not exactly. I thought I followed the instructions from the CodeIgniter config correctly but apparently not! As the documentation differs slightly, you must in fact ADD

$config['sess_table_name'] = 'ci_sessions';

To your config.php file. Once that has been added CodeIgniter will correctly store user data in that table name provided.

0
votes

Note: The Session class has built-in garbage collection which clears out expired sessions so you do not need to write your own routine to do it.

It means you no need to insert it Manually. It will execute automatically.


In config/config.php change this

$config['sess_use_database']    = FALSE;

to this

$config['sess_use_database']    = TRUE;

and session table should be

CREATE TABLE IF NOT EXISTS  `ci_sessions` (
    session_id varchar(40) DEFAULT '0' NOT NULL,
    ip_address varchar(45) DEFAULT '0' NOT NULL,
    user_agent varchar(120) NOT NULL,
    last_activity int(10) unsigned DEFAULT 0 NOT NULL,
    user_data text NOT NULL,
    PRIMARY KEY (session_id),
    KEY `last_activity_idx` (`last_activity`)
);

check Session table name is

$config['sess_table_name'] = 'ci_sessions';