Note that there are some explanatory texts on larger screens.

plurals
  1. POforeach loop throwing an error
    primarykey
    data
    text
    <p>I am passing an array into a php page from a js call(ajax) and its throwing an error . The line of code that the error is in is:</p> <pre><code> foreach ($array as list($a, $b, $c, $d, $e, $f, $g, $h)) { </code></pre> <p>It's my first attempt at pdo.</p> <p>Can anyone see what is wrong with the code?</p> <p><strong>js &amp; ajax call:</strong></p> <pre><code> var outerarray=[]; var innerarray = []; innerarray[0]="1"; innerarray[1]="2"; innerarray[2]="3"; innerarray[3]="4"; innerarray[4]="5"; innerarray[5]="6"; innerarray[6]="7"; innerarray[7]="8"; innerarray[8]="9"; innerarray[9]="10"; innerarray[10]="11"; innerarray[11]="1"; innerarray[12]="13"; outerarray[0]=innerarray; $.ajax({ type: "POST", url: "ajax/upadateElements.php", data: {outerarray:outerarray}, success:function(data){ alert("data:"+data); }, error: function(data){ alert("failure"); } }); </code></pre> <p><strong>updateElements.php code</strong></p> <pre><code> $db = new PDO('mysql:host=localhost;dbname=dbname;charset=utf8', username, password, array(PDO::ATTR_EMULATE_PREPARES =&gt; false, PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION)); $array[]=$_POST['outerarray']; foreach ($array as list($a, $b, $c, $d, $e, $f, $g, $h)) { $stmt = $db-&gt;prepare("SELECT * FROM table WHERE a=:a AND b=:b"); $stmt-&gt;execute(array(':a' =&gt; $a, ':b' =&gt; $b)); $row_count = $stmt-&gt;rowCount(); if($row_count==0){ $stmt = $db-&gt;prepare("INSERT INTO table(a, b, c, d, e, f, g, h) VALUES(:field1,:field2,:field3,:field4,:field5,:field6,:field7,:field8)"); $stmt-&gt;execute(array(':field1' =&gt; $a, ':field2' =&gt; $b, ':field3' =&gt; $c, ':field4' =&gt; $d, ':field5' =&gt; $e, ':field6' =&gt; $f, ':field7' =&gt; $g, ':field8' =&gt; $h)); $affected_rows = $stmt-&gt;rowCount(); } if($affected_rows !=0){ echo "success"; }else{ echo "fail"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload