Unknown column 'data' in 'field list' in codeigniter - codeigniter

I am creating one website using codeigniter with session. When i load the welcome page in codeigniter its showing the error like:-
Error Number: 1054, Unknown column 'data' in 'field list', SELECT
data FROM ci_sessions WHERE id =
'562a04f4kt6j4n6eehsob3vm3puuc3r5', Filename:
C:/xampp/htdocs/ci_ecom/system/database/DB_driver.php, Line Number:
691.
I call the session library and database library in auto load area corretly. I dont know exactly where i did the mistake I check it in C:/xampp/htdocs/ci_ecom/system/database/DB_driver.php this path area also..
In my table I put the column name user_data but here its showing Unknown column 'data' in 'field list'.
this is my table stucture
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`)
);
This is my ci session
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
any one can u pls help what is the exact problem...

A straight forward answer is to create the table in this format,
CREATE TABLE IF NOT EXISTS `ci_sessions` (
`id` varchar(40) NOT NULL,
`ip_address` varchar(45) NOT NULL,
`timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
`data` blob NOT NULL,
PRIMARY KEY (id),
KEY `ci_sessions_timestamp` (`timestamp`));
and everything should go fine. It's because the session table structure is modified in the latest versions of codeigniter.

I believe you'd experience this problem in CodeIgniter 3.X. To resolve it: Update the config.php to look like below.
$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_session';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

Your ci_sessions table does not have a data field, yet, in a query not shown in the question you try to load it or write it.

Related

Why Laravel does not insert row?

Inserting operation is:
$r = [
'idVisitor' => 4,
'lastname' => 'fg',
'firstname' => '11',
'document_number' => '444',
'idEvent' => 2
];
$g = Visitor::insert($r);
Model is:
class Visitor extends Model
{
public $timestamps = false;
protected $table = 'visitors';
protected $fillable = ['firstname', 'lastname', 'middlename', 'document_number', 'pincode', 'ckecked', 'date_cheked', 'user_cheked', 'company', 'code', 'status', 'idEvent', 'date', 'birthday'];
protected $primaryKey = 'idVisitor';
}
if to do this:
$g = Visitor::insert($r);
var_dump($g);
It gives me true, so Laravel considers that it was committed.
Dump SQL is:
CREATE TABLE `visitors` (
`idVisitor` int(11) NOT NULL,
`firstname` varchar(45) NOT NULL,
`lastname` varchar(45) NOT NULL,
`middlename` varchar(45) DEFAULT NULL,
`document_number` varchar(45) NOT NULL,
`pincode` varchar(10) DEFAULT NULL,
`ckecked` int(11) DEFAULT '0',
`date_cheked` date DEFAULT NULL,
`user_cheked` int(11) DEFAULT NULL,
`company` varchar(100) DEFAULT NULL,
`code` varchar(100) DEFAULT NULL,
`idEvent` int(11) NOT NULL,
`status` int(11) NOT NULL DEFAULT '1',
`date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`birthday` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `visitors`
--
ALTER TABLE `visitors`
ADD PRIMARY KEY (`idVisitor`,`idEvent`),
ADD UNIQUE KEY `document_number_UNIQUE` (`document_number`),
ADD UNIQUE KEY `pincode_UNIQUE` (`pincode`),
ADD UNIQUE KEY `code_UNIQUE` (`code`),
ADD KEY `fk_visitors_users1_idx` (`user_cheked`),
ADD KEY `fk_visitors_events1_idx` (`idEvent`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `visitors`
--
ALTER TABLE `visitors`
MODIFY `idVisitor` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=18;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `visitors`
--
ALTER TABLE `visitors`
ADD CONSTRAINT `fk_visitors_events1` FOREIGN KEY (`idEvent`) REFERENCES `events` (`idEvent`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_visitors_users1` FOREIGN KEY (`user_cheked`) REFERENCES `users` (`idUser`) ON DELETE NO ACTION ON UPDATE NO ACTION;
COMMIT;
I use MySQL 5.0.12
Also I tried:
$g = new Visitor();
$g->lastname = 4;
$g->middlename = 4;
$g->document_number = 444;
$g->firstname = 4;
$g->idEvent = 2;
$g->save();
please use
$g = new Visitor();
$g->idVisitor = 4;
$g->firstname= 4;
$g->idVisitor = 4
$g->save();
this is another way
$g = new Visitor(); call class without '()', so $g = new Visitor;

How to save INT fields as null in laravel

I'm getting an error when saving some empty text, textarea fields. Laravel forms this sql query:
SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'inside_area' at row 1 (SQL: insert into `ads` (`street`, `quarter`, `building_number`, `inside_area`, `price`, `admin_comment`, `valid_to`, `price_heating`, `rooms`, `floor_number`, `floors_number`, `kitchen_area`, `years`, `public_comment`, `video_url`, `3d_url`, `user_id`, `updated_at`, `created_at`) values (, , , , , , , , , , , , , , , , 1, 2017-03-13 14:33:50, 2017-03-13 14:33:50))
P.S. db table was created not in laravel way - I'm using existing tables, this may be important.
UPDATED: problem are only with INT fields, if they has empty form field on saving!
Table:
CREATE TABLE `ads` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`ad_type_id` int(11) DEFAULT NULL,
`city_id` int(11) DEFAULT NULL,
`street` varchar(255) DEFAULT NULL,
`quarter` varchar(255) DEFAULT NULL,
`building_number` varchar(255) DEFAULT NULL,
`inside_area` int(11) DEFAULT NULL,
`price` int(11) DEFAULT NULL,
`show_price_per_meter` int(1) DEFAULT NULL,
`price_heating` int(10) DEFAULT NULL,
`admin_comment` text,
`valid_to` datetime DEFAULT NULL,
`rooms` int(11) DEFAULT NULL,
`floor_number` int(11) DEFAULT NULL,
`floors_number` int(11) DEFAULT NULL,
`kitchen_area` int(11) DEFAULT NULL,
`balcony` int(1) DEFAULT NULL,
`balcony_glazed` int(1) DEFAULT NULL,
`years` int(11) DEFAULT NULL,
`dyn_house_type_id` int(11) DEFAULT NULL,
`dyn_heating_id` int(11) DEFAULT NULL,
`dyn_installation_ids` int(11) DEFAULT NULL,
`public_comment` text,
`video_url` varchar(11) DEFAULT NULL,
`3d_url` varchar(11) DEFAULT NULL,
`available_for_trade` int(1) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
FORM:
{!! Form::open(['url'=>'ads']) !!}
{!! Form::number('inside_area', null, ['class'=>'form-control']) !!}
{!! Form::close() !!}
Route:
Route::resource('ads', 'AdsController');
Save action:
public function store() {
$input = Request::all();
\App\Ad::create($input);
return redirect('/ads/my_index');
}
P.S.2 If I provide any value to inside_area field, it goes ok and the next error is for price field.
You can use attribute mutator for each integer attribute. In these mutators you can prepare data before inserting into DB.
In your case you should create mutators in your model Ads for every field with the type INT:
// define a mutator for the field "inside_area"
public function setInsideAreaAttribute($value)
{
$this->attributes['inside_area'] = (int) $value;
}
I understand that creating such a mutator for each INT field is boring, but it will also grant you ability to control user input before inserting into DB.
You can find more information about mutators here (for the latest version of Laravel at this moment):
Laravel 5.4, Defining a Mutator:
https://laravel.com/docs/5.4/eloquent-mutators#defining-a-mutator
Perhaps, you are passing string data to 'inside_area' variable. Because there is more default null fields at your table, like 'city_id', 'ad_type_id'
another way is to use Iatstuti and provide in Ad.php model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Iatstuti\Database\Support\NullableFields;
class Ad extends Model {
use NullableFields;
public $timestamps = true;
protected $nullable = [
'inside_area'
];
protected $fillable = [
'user_id',
'ad_type_id',
'inside_area'
];
}

where_not_in not return match by array value as param

trying to get TraineeID where $arrTID not in tbl_attendance_processed but my code below return all TraineeID from tbl_attendance_processed.
If I use where_in then the query return absolute result those TraineeID are really matched. Just not returning appropriate when I use where_not_in.
Model:
public function getNotMatch_TID($arrTID,$atnDate){
$this->db->select('TraineeID');
$this->db->where_not_in('TraineeID', $arrTID);
$this->db->where('attnDate', $atnDate);
$query = $this->db->get('tbl_attendance_processed');
return $query->result();
}
controller:
$arrTID = $this->input->post('Present');
$atnDate = $this->input->post('attnDate');
$nonMatch= $this->AttendanceModel->getNotMatch_TID($arrTID,$atnDate);
print_r($nonMatch);
echo last query:
table schema:
CREATE TABLE `tbl_attendance_processed` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`TraineeID` varchar(7) CHARACTER SET utf8 NOT NULL,
`attnDate` date NOT NULL,
`inTime` time NOT NULL,
`outTime` time NOT NULL,
`Status` varchar(10) NOT NULL,
`ClassCount` tinyint(1) NOT NULL DEFAULT '1',
`Reason` varchar(255) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3186 DEFAULT CHARSET=latin1

$config['sess_use_database'] = TRUE Then set_flashdata() is not working properly in codeigniter

When i set
$config['sess_use_database'] = TRUE;
Then after if i am using
$this->session->set_flashdata('message','record updated');
It will be destroyed all session data.
If i am doing false
$config['sess_use_database'] = TRUE;
Then set_flashdata() is working and does not destroyed session data.
Please give me advice how we can resolve this issue.
Thanks in advance.
You can select between the "Files driver" (default) and the "Database driver".
When you want to use the Database driver edit two lines in the config:
$config['sess_driver'] = 'database'; // select database driver
$config['sess_save_path'] = 'ci_sessions'; // name of the mysql table
Session table (Codeigniter 3.x):
Edit in config.php
$config['sess_driver'] = 'database'; // select database driver
$config['sess_save_path'] = 'ci_sessions'; // name of the mysql table
Create table:
CREATE TABLE IF NOT EXISTS `ci_sessions` (
`id` varchar(40) NOT NULL,
`ip_address` varchar(45) NOT NULL,
`timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
`data` blob NOT NULL,
PRIMARY KEY (id),
KEY `ci_sessions_timestamp` (`timestamp`)
);
Session table (Codeigniter 2.x):
Edit in config.php
$config['sess_use_database'] = TRUE;
$config['sess_save_path'] = 'ci_sessions'; // name of the mysql table
Create table:
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`)
);

Codeigniter get sum of goals a player has made

I got a database. This is the mysql script of it. A player can appear more in the table what I want to do is get the sum of all the goals this player has made.
Script:
CREATE TABLE IF NOT EXISTS `spelerswedstrijden` (
`Id` int(5) NOT NULL AUTO_INCREMENT,
`spelerId` int(2) NOT NULL,
`wedstrijdId` int(5) NOT NULL,
`goals` int(5) DEFAULT NULL,
`assisten` int(5) DEFAULT NULL,
`gelekaarten` int(5) DEFAULT NULL,
`rodekaarten` int(5) DEFAULT NULL,
PRIMARY KEY (`Id`)
)
My Model code:
function getGoals($id) {
$this->db->where('spelerId', $id);
$this->db->from('spelerswedstrijden');
$goals = $this->db->select_sum('goals');;
return $goals;
}
Using this code gives me following error: Object of class CI_DB_mysql_driver could not be converted to string
Any Ideas how I can fix this?
Thanks in advance
function getGoals($id)
{
$this->db->select('COUNT(id) as TotalGoals');
$this->db->where('spelerId', $id);
$this->db->group_by('spelerId');
$goals = $this->db->get('goals');;
return $goals->row();
}
In controller access it like this
$result = $this->mymodel->getGoals($id);
echo $result->TotalGoals;
try;
function getGoals($id) {
$this->db->select_sum('goals');
$this->db->where('spelerId', $id);
$this->db->from('spelerswedstrijden');
$goals = $this->db->get();
return $goals;
}
Another Appraoch:
$this->db->select('sum(goals) AS total_goals');

Resources