Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a snippet that does what you want:</p> <pre><code>//database connection $db = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass); //query $sql = "INSERT INTO table (c1, c2, c3, c4) VALUES (:c1, :c2, :c3, :c4)"; $stmt = $db-&gt;prepare($sql); $stmt-&gt;execute(array( ':c1'=&gt;$data_t1[0], ':c2'=&gt;$data_t2[0], ':c3'=&gt;$data_t3[0], ':c4'=&gt;$data_t4[0], )); //this is for row1: +--------------------+ | 12 | 4 | 6 | 1.33 | +--------------------+ +++++++++++++++++++++++++++++++++++++++++ //and for row2 $stmt-&gt;execute(array( ':c1'=&gt;$data_t1[1], ':c2'=&gt;$data_t2[1], ':c3'=&gt;$data_t3[1], ':c4'=&gt;$data_t4[1], )); //this is for row2: +----------------------+ | 12 | 3 | 5 | 1.44 | +----------------------+ </code></pre> <p><strong>NB:</strong> I didn't do a test run of this code so you might encounter some errors.</p> <p><strong>EDIT</strong></p> <p>Using Barmar's idea just add extra columns <code>c5, c6, c7, c8</code> and bind them to new values <code>$data_t11[$i], $data_t22[$i], $data_t33[$i], $data_t44[$i]</code></p> <pre><code>$sql = "INSERT INTO table (c1, c2, c3, c4, c5, c6, c7, c8) VALUES (:c1, :c2, :c3, :c4, :c5, :c6, :c7, :c8)"; $stmt = $db-&gt;prepare($sql); foreach ($data_t1 as $i =&gt; $value) { $stmt-&gt;execute(array( ':c1'=&gt;$data_t1[$i], ':c2'=&gt;$data_t2[$i], ':c3'=&gt;$data_t3[$i], ':c4'=&gt;$data_t4[$i], ':c5'=&gt;$data_t11[$i], ':c6'=&gt;$data_t22[$i], ':c7'=&gt;$data_t33[$i], ':c8'=&gt;$data_t44[$i], )); } </code></pre>
 

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