Anand Singh
Anand Singh

Reputation: 2363

MySQL behaving oddly while inserting rows

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

Answers (4)

Abdulla Nilam
Abdulla Nilam

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

  1. Phpmyadmin
  2. select your database, then go to users table.
  3. Click structure on top.
  4. Click Change, in front of id field.
  5. Then there is check box with AI(Auto Increment)
  6. Check that and click save

Upvotes: 0

AbdulBasit
AbdulBasit

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

Danyal Sandeelo
Danyal Sandeelo

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

Ricky
Ricky

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

Related Questions