MySQL codeigniter INSERT 查询正确并执行但未执行插入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16647112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
codeigniter INSERT query is correct and executed but no insert is performed
提问by Lucabro
relevant code:
相关代码:
$sql = "INSERT INTO rel_attivita_corsi(id_attivita,id_corso) VALUES";
foreach($elements as $element) {
$sql .= "('".$element."','1'),";
}
$sql = substr($sql,0,-1);
if (!$this->db->query($sql)) {
echo "FALSE";
}
else {
echo "TRUE";
}
echo $this->db->last_query();
The table structure:
表结构:
rel_attivita_corsi
-----------------------------
ID (int) primary
id_attivita (int)
id_corso (int)
I'm using codeigniter, as you can see, the last raw return the right query, but the table in the db remain empty... running the query returned in phpmyadmin everything works correctly.
我正在使用 codeigniter,如您所见,最后一个原始返回正确的查询,但数据库中的表保持为空...运行 phpmyadmin 中返回的查询一切正常。
Any ideas?
有任何想法吗?
UPDATE 1 - Using active records:
更新 1 - 使用活动记录:
$dati = array();
foreach($elements as $element){
$dati[] = array('id_attivita' => $element, 'id_corso' => 1);
}
if (!$this->db->insert_batch("rel_attivita_corsi",$dati)) {
echo "FALSE";
}
else {
echo "TRUE";
}
echo $this->db->last_query();
no success, last query printed correctly but no insert happen
没有成功,最后一个查询打印正确,但没有插入
UPDATE 2 - Using active records and no foreach:
更新 2 - 使用活动记录并且没有 foreach:
$this->db->insert_batch("rel_attivita_corsi",array(array('id_attivita' => 7,'id_corso' => 1),array('id_attivita' => 9,'id_corso' => 1)));
no success....
没有成功....
i've substituted fake value with real now, and the $elementsarray is:
我现在用真实值替换了假值,$elements数组是:
Array
(
[0] => 7
[1] => 9
)
UPDATE 3Problem solved... there was another query after the code that deleted every record insert into the table, so the table was always empty
更新 3问题解决了...在删除每条记录插入表的代码之后还有另一个查询,所以表总是空的
If you heve doubt on the flow of your code try use profiling
如果您对代码的流程有疑问,请尝试使用分析
回答by itsme
i think you are looping wrong:
我认为你循环错了:
$sql .= "('".$element."','1'),";
this produces (value,1), (value, 1) , (value, 1)
这产生 (value,1), (value, 1) , (value, 1)
while you need
当你需要
(value,value,value)
ma che te lo dico a fare poi :P
ma che te lo dico a fare poi :P
so your query is:
所以你的查询是:
INSERT INTO table(ID,id_activity,id_cont) VALUES (somenthin,1), (somenthing,1) etc ..
and you need instead
而你需要
INSERT INTO table(ID,id_activity,id_cont) VALUES (value,value,value) ;
you can try this:
你可以试试这个:
$sql = "INSERT INTO table(id_activity,id_cont) VALUES (".implode(',',$elements)."); ";
if (!$this->db->query($sql)) {
echo "FALSE";
}
else {
echo "TRUE";
}
echo $this->db->last_query();
IN CASE you are trying making an INSERT BATCH check the Codeigniter documentation it says:
如果您正在尝试制作 INSERT BATCH 检查 Codeigniter 文档,它说:
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name' ,
'date' => 'Another date'
)
);
$this->db->insert_batch('mytable', $data);
// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')
回答by Lucabro
replace the code :
替换代码:
$sql = "INSERT INTO table(id_activity,id_cont) VALUES";
foreach($elements as $element) {
$sql .= "('".$element."','1'),";
}
With the code:
使用代码:
$sql = "INSERT INTO table(ID,id_activity,id_cont) VALUES";
foreach($elements as $element) {
$sql .= " ('','".$element."','1'),";
}

