1

Hi guys i was trying whole my week to solve this problem. I want to set my tables "isim" column with array. But the bot fills this column with array[1] in all rows. I want to set each row with each element of array but its filled with the same element always. This is the relevant code:

for($a=0; $a<=29; $a++){
//echo ($dizi[1][$a]."<br />"); 
foreach (array($dizi[1][$a]) as $dizipub2){ 
$query = $db->prepare("UPDATE diziler SET diziisim=?");
$Insert = $query->execute(array("$dizipub2" 
 )); break;

if ($Insert){   
    $last_id = $db->lastInsertId();
echo ("Kaydedildi");break;}         
}  

I've tried deleting // before echo and what is the output of it. It's correct. Gives elements perfectly. I guess something wrong with foreach. How can i do that?

Tarık Baysal
  • 41
  • 1
  • 5
  • 1
    This simply is not how the SQL language works. You cannot make up your own commands and expect them to work. Please start reading the documentation of the tools you use! SQL does not know anything about arrays. It works on scalar values only. – arkascha Nov 29 '16 at 13:30
  • your foreach is creating an array of $dizi[1][$a], is that correct? you should use foreach($dizi[1][$a] as $dizipub2), but only if $diz[1][$a] returns an array. Also, you put some breaks inside your foreach, the last one is useless, and the middle one will prevent your if($insert) test to run – leoap Nov 29 '16 at 13:45
  • understood that break is useless thx. Yes it is creating an array and it returns an array everything fine without using db – Tarık Baysal Nov 29 '16 at 21:32
  • @arkascha i learnt that but everybody says in this way. Also read documents about sql but it doesnt explains anything about insert arrays to table in my way. I couldnt find anything else also tried a few methods on my own which are sadly dont work:( could you tell in example ? – Tarık Baysal Nov 29 '16 at 21:36
  • Sorry, but what do you expect me to add? I said all there is to say: SQL does not support arrays. Period. – arkascha Nov 29 '16 at 21:39
  • what can i do ? is there any solution for this ? – Tarık Baysal Dec 04 '16 at 17:36

1 Answers1

0

Try this

for($a=0; $a<=29; $a++){
    //echo ($dizi[1][$a]."<br />");
    foreach (array($dizi[$a]) as $dizipub2){
        $query = $db->prepare("UPDATE diziler SET diziisim='$dizipub2'");
        $Insert = $query->execute();
        break;

        if ($Insert){
            $last_id = $db->lastInsertId();
            echo ("Kaydedildi");
            break;
        }
    }
}
leoap
  • 1,684
  • 3
  • 24
  • 32
kumaravel
  • 74
  • 4
  • Its not a good practice to insert variables direct in the SQL string. You should use parameters – leoap Nov 29 '16 at 13:42
  • and also if i dont use dizi[1][$a] it wont give me anything the "[1]" is absolute thing in my arrays. Btw thanks for answer – Tarık Baysal Nov 29 '16 at 21:34