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
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:
public function addemail(){
ini_set('display_errors','off');
$this->load->model("Model_email","", true);
$this->Model_email->addemail($_POST["email"]);
}
public function addemail($email){
$sql = "INSERT INTO emails(email)VALUES(?)";
$data = array($email);
$this->db->query($sql,$data);
return $this->db->insert_id();
}
<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.
Try this:
public function addemail(){
//ini_set('display_errors','off');
$this->load->model('Model_email');
$this->Model_email->addemail($this->input->post('email'));
}
try:
var_dump($this->input->post(NULL, TRUE));
to view what is submitted, by then you will see what is submitted by POST.
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"]);
}
}
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')