问题描述
我试图使选项更新三表一个执行我的CI与sql那里,但为什么它的仍然是错误?
这是错误警告:
发生数据库错误
错误号码:1062
键1的重复条目'0'
UPDATE`t_publisher` SET`id_publisher` = NULL,`publisher` = NULL,`artis` = NULL,`id_label` = NULL WHERE`id_publisher `='113'
这是代码:
函数更新($ 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)
}
我的代码有什么问题?请帮忙。感谢。
您的 UPDATE
$ c> id_publisher 列到 NULL
,并根据列的名称和您收到的错误, / em>列是表的 PRIMARY KEY
,设置为无符号NOT NULL
。
id_publisher = NULL
时,MySQL会将其转换为 id_publisher = 0
由于 unsigned
部分。这将在第一次执行时执行,但是当您在第二列执行时,您将尝试插入 主键值 0
,这是不允许的。 根据您的示例中 die()
语句的位置代码,我假设以下块是罪魁祸首:
$ 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);
这里, $ id_publis
空或空。
我建议从 id_publisher = NULL > UPDATE 子句,就像删除'id_publisher'=>数组中的$ id_publis,
,或重新思考你实际需要将其设置为 null $的原因c $ c>开始(在这种情况下,会删除该行更有益吗?)
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.
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?)
这篇关于数据库错误发生错误编号:1062的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!