0

I have PHP code that gets multiple arrays:

<?php
$checkKey = $_POST['key'];

if ($key === $checkKey)
{

  $a = array_values($_POST['a']);
  $b = array_values($_POST['b']);
  $c = array_values($_POST['c']);
  $d = array_values($_POST['d']);

  if ( (count($a) !== count($b)) || (count($a) !== count($c)) || (count($a) !== count($d)) )
  {
    die ('Not enough parameters!');
  }
  else
  {

  }


}
?>

I want to iterate over all the arrays an build a SQL INSERT query like this:

INSERT INTO xyz (a,b,c,d) VALUES (1,2,3,4), (4, 5, 6, 7);

Where the values are stored in each array (i.e. in this example a contains the values 1 and 4, b the values 2 and 5, etc.)

How can I achieve that?

Fischer Ludrian
  • 629
  • 1
  • 9
  • 23
  • 2
    Sidenote (*a look ahead*): If and when you do get someone to give you a working answer, do remember that `key` and `keys` are [MySQL reserved words](http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html). – Funk Forty Niner May 12 '14 at 13:42
  • 1
    I presume that `$key` is defined somewhere? You might want to check that $a, $b, $c, and $d are set (`isset()`) before using, unless you can guarantee that all are present. Why are you using "not-equivalent" `!==` instead of "not-equal" `!=` when comparing the count()'s? SQL _does_ have an array type -- are you using that? – Phil Perry May 12 '14 at 13:46
  • It would be much simpler and cleaner using abstract models or prepared statements, or some kind of query builder. – Daniel W. May 12 '14 at 13:46

4 Answers4

1

Just use a for() loop to iterate over the indexes from 0 to count($a) (you could use any of the arrays, they checked to be the same size).

$sql = 'insert into xyz (a, b, c, d) values ';
for ($i = 0, $l = count($a); $i < $l; ++$i) {
     $sql .= "('".
         // it's really important to escape the input!
         mysqli_real_escape_string($link, $a[$i]).','.
         mysqli_real_escape_string($link, $b[$i]).','.
         mysqli_real_escape_string($link, $c[$i]).','.
         mysqli_real_escape_string($link, $d[$i]).
     "'), ";
}
$sql = substr($sql, 0, -2); // trim down the last ', '
complex857
  • 20,425
  • 6
  • 51
  • 54
1

use this:

function transpose($array) {
    array_unshift($array, null);
    return call_user_func_array('array_map', $array);
}
//example
$a=array(1,3);
$b=array(2,4);
$c=array(5,4);
$d=array(10,12);

$r= transpose(array($a,$b,$c,$d));
$sql='INSERT INTO xyz (a,b,c,d) VALUES ';
foreach($r as $values){
$sql.='('.implode(',',$values).'),';
}
$sql=rtrim($sql,',');
echo $sql;
laurent
  • 418
  • 3
  • 7
0

I think there is no build-in php function so you can just do this:

<?PHP

$count = count($a);
$sqlout = 'INSERT INTO xyz (a,b,c,d) VALUES ';

for($i=0; $i<$count; $i++)
{
   $sqlout .= ( $i>0 ? ',' : '') . '('. $a[$i] .', '. $b[$i] .', '. $c[$i] .', '. $d[$i] .') ';
}


?>

and make sure to escape your input to avoid SQL Injection!

Thomas
  • 2,345
  • 1
  • 18
  • 17
0

Simple loop around

<?php
$checkKey = $_POST['key'];

if ($key === $checkKey)
{

    $a = array_values($_POST['a']);
    $b = array_values($_POST['b']);
    $c = array_values($_POST['c']);
    $d = array_values($_POST['d']);

    if ( (count($a) !== count($b)) || (count($a) !== count($c)) || (count($a) !== count($d)) )
    {
        die ('Not enough parameters!');
    }
    else
    {
        $sql_array = array();
        foreach($a AS $key=>$values)
        {
            $sql_array[] = "(".(int)$a[$key].",".(int)$b[$key].",".(int)$c[$key].",".(int)$d[$key].")";
        }
        if (count($sql_array) > 0)
        {
            $sql = "INSERT INTO xyz (a,b,c,d) VALUES ".implode(",", $sql_array);
            // Execute it here
        }
    }


}
?>
Kickstart
  • 21,403
  • 2
  • 21
  • 33