Reputation: 2363
I am seeing an odd behavior of MySQL data base.While inserting a row in database with codeigniter, row is not persist but auto increment field is incremented. I can see increment when I insert a new row. And the worst is that if a insert same data again (submitting form), it get inserted and I can see that data in database.
My code:
$sql = "INSERT INTO `users`(`first_name`, `country`, `username`, `password`, `email`, `ip_address`, `created_on`, `active`)
VALUES('".$adiData['first_name']."', '".$adiData['country']."', '".$username."', '".$password."', '".$email."', '".$ip_address."', ".$time.", 1)";
$this->db->query($sql);
One more thing which makes me more confuse, yesterday I restart server and insert get fine. and now this morning I am getting same trouble.
Please tell what could be the reason?
Upvotes: 0
Views: 83
Reputation: 38609
Codeigniter Insert Method
$data = array(
'first_name' => $this->input->post('first_name'),
'country' => $this->input->post('country'),
'username' => $username,
'password' => $password,
'email' => $email,
'ip_address' => $ip_address,
'time' => $time,
);
$this->db->insert('users', $data);
and in config/database.php
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';//MySQL Username
$db['default']['password'] = '';//MySQL Password
$db['default']['database'] = '';//db name
and in config/autoload.php
$autoload['libraries'] = array('database');
Make Sure variables are not empty
EDIT 01
Goto
database
, then go to users
table.structure
on top.Change
, in front of id
field.Upvotes: 0
Reputation: 1409
Make sure your createdOn field type is set to datetime, and check the format of the data being passed to this field. If the datetime is not in correct format you may face this issue.
Upvotes: 0
Reputation: 12391
You need to have unique constraints, data redundancy is a very issue, you must not to make some columns unique or may be composite key, depends on your requirements.
For example:
In case of a user table, the username must be unique so it won't let you insert redundant rows.
Upvotes: 1
Reputation: 568
Try using in codeigniter way For ex.
// get data from form
$data = array('name'=>$this->input->post('name'),
'email'=>$this->input->post('email'),
'contact'=>$this->input->post('contact'),
'address'=>$this->input->post('address')
);
$this->db->insert('tableName',$data);
For more read this http://w3code.in/2015/10/how-to-insert-and-select-data-from-database-in-codeigniter-beginner-guide/
Upvotes: 0