Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

When I am trying to insert a new value in a MySQL database, I have the following error:


Error Number: 1048

Column 'email' cannot be null

INSERT INTO emails(email)VALUES(NULL)

Filename: models/Model_email.php

Line Number: 7


I had not this error when I was running the website on the local server, the error appeared on production. I code in PHP with the framework CodeIgniter.

The code is pretty simple though:


Controler.php

public function addemail(){

    ini_set('display_errors','off');
    $this->load->model("Model_email","", true);
    $this->Model_email->addemail($_POST["email"]); 

}

Model_email.php

public function addemail($email){
    $sql = "INSERT INTO emails(email)VALUES(?)";
    $data = array($email);
    $this->db->query($sql,$data);
    return $this->db->insert_id();
}

HTML form

<form method="post" action="<?=site_url("/Controler/addemail")?>">
<input type="text" name="email">
<input type="submit">
</form>

When I add a var_dump($email) in the function in Model_email.php or a var_dump($_POST["email"]) in Controler.php, both page shows a NULL. However the method post in the form seems to be right.

share|improve this question
    
I'm gonna try to answer your question, but please use better code conventions. Either go with camel case or snake case and stick with it. You shouldn't be using ini_set inside your function anyway. Unless that's called at the beginning of script execution it won't even do anything. Have you verified that there is in fact a value in $_POST['email']? – Zarathuztra May 12 '15 at 21:17
    
The error message says what is the error. – Shaiful Islam May 12 '15 at 21:34

Try this:

public function addemail(){
    //ini_set('display_errors','off');
    $this->load->model('Model_email');
    $this->Model_email->addemail($this->input->post('email'));
}
share|improve this answer

try:

var_dump($this->input->post(NULL, TRUE));

to view what is submitted, by then you will see what is submitted by POST.

share|improve this answer

Try this

public function addemail(){
    //ini_set('display_errors','off');
    if(isset($_POST["email"]) && $_POST["email"] && !empty($_POST["email"])){
         $this->load->model("Model_email","", true);
         $this->Model_email->addemail($_POST["email"]); 
    }
}
share|improve this answer

Use this one :

$data = array( 'email' => $email

);

$this->db->insert('tablename', $data);

// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.