Database Error Occurred Error Number: 1062

2020-05-02 03:29发布

问题:

i tried to make option update three table with one execution for my CI with sql there, but why its still error?

this is the error warning:

A Database Error Occurred

Error Number: 1062

Duplicate entry '0' for key 1

UPDATE `t_publisher` SET `id_publisher` = NULL, `publisher` = NULL, `artis` = NULL, `id_label` = NULL WHERE `id_publisher` = '113'

this is the code:

function update($id_user=null)
  {
   if (($this->input->post('submit') == 'Update')){
    $user=$this->input->post('username');
    $pass=$this->input->post('userpassword');
    $ussta=$this->input->post('userstatus');
    $usty=$this->input->post('usertype');

   $data = array(
    'user_name' => $user,
    'user_pass' => $pass,
    'user_status' => $ussta,
    'user_type' => $usty);

    $this->db->where('user_id', $this->input->post('id'), $data);
    $this->db->update("t_user",$data);

   $data1 = array(
    'id_publisher' => $id_publis,
    'publisher' => $publis,
    'artis' => $ar,
    'id_label' => $id_lab);

    $this->db->where('id_publisher', $this->input->post('id'), $data);
    $this->db->update("t_publisher",$data1);
    echo $this->db->last_query();
    die();

   $data2 = array(
    'id_label' => $id_lab,
    'label' => $label);

    $this->db->where('id_label', $this->input->post('id'), $data);
    $this->db->update("t_label",$data2);
    echo $this->db->last_query();
    die();
    redirect("registrasi/reg");
   }
    $var['data'] = $this->db->query("select * from t_user where USER_ID= '$id_user'")->row_array();  
    $var1['data'] = $this->db->query("select * from t_publisher where id_publisher = '$id_publis'")->row_array();
    $var2['data'] = $this->db->query("select * from t_label where id_label = '$id_lab'")->row_array();
    $this->load->view('update', $var,$var1,$var2);
}

whats wrong with my code? please help. thanks before.

回答1:

Your UPDATE clause is setting the id_publisher column to NULL, and, based on the name of the column and the error you're receiving, that column is the table's PRIMARY KEY with a setting of unsigned NOT NULL.

Because of this, when you do id_publisher = NULL, MySQL converts it to id_publisher = 0 due to the unsigned part. This will execute fine the first time, however, when you run it on a second row you will now be attempting to insert a second primary-key value of 0, which is not allowed.

Based on the location of the die() statement in your sample code, I'm assuming the following block is the culprit:

   $data1 = array(
    'id_publisher' => $id_publis,
    'publisher' => $publis,
    'artis' => $ar,
    'id_label' => $id_lab);

    $this->db->where('id_publisher', $this->input->post('id'), $data);
    $this->db->update("t_publisher",$data1);

Here, your $id_publis variable is either empty or null.

I would suggest to either remove the id_publisher = NULL portion from the UPDATE clause which is as simple as removing 'id_publisher' => $id_publis, from the $data1 array, or rethink the reason you actually need to set it to null to begin with (in this case, would deleting the row be more beneficial?)