mysql - codeigniter INSERT query is correct and executed but no insert is performed -


relevant code:

$sql = "insert rel_attivita_corsi(id_attivita,id_corso) values"; foreach($elements $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, can see, last raw return right query, table in db remain empty... running query returned in phpmyadmin works correctly.

any ideas?

update 1 - using active records:

$dati = array();  foreach($elements $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 no insert happen

update 2 - using active records , no 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 real now, , $elements array is:

array (     [0] => 7     [1] => 9 ) 

update 3 problem solved... there query after code deleted every record insert table, table empty

if heve doubt on flow of code try use profiling

i think looping wrong:

$sql .= "('".$element."','1'),"; 

this produces (value,1), (value, 1) , (value, 1)

while need

(value,value,value) 

ma che te lo dico fare poi :p

so query is:

insert table(id,id_activity,id_cont) values (somenthin,1), (somenthing,1) etc .. 

and need instead

insert table(id,id_activity,id_cont) values (value,value,value) ; 

you can try this:

$sql = "insert 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 trying making insert batch check codeigniter documentation says:

 $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 mytable (title, name, date) values ('my title', 'my name', 'my date'), ('another title', 'another name', 'another date') 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -