Question

I'm passing an html array to php using jquery serializearray() function.

In php I can access the array using $_POST like

 $a = $_POST['htmlarray']

The html array, however, is an array of arrays like so

 htmlarray[] = [[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18]]

I want to format the variable $a so that I can insert all the html array values in one single insert query like

 INSERT INTO table
 (val1, val2, val3, val4, val5, val6)
  VALUES
    (1,2,3,4,5,6),
   (7,8,9,10,11,12),
    (13,14,15,16,17,18)

I know I have to use an implode function, can anyone show how this could be done.

Was it helpful?

Solution

I'm not quite sure what an html array is, but try the following:

$a = $_POST['htmlarray'];

// unserialize $a

// build sql query up to '...VALUES '

foreach ($a as $row) {
   $sql .= '(';
   $sql .= implode(',', $row);
   $sql .= ')',
}

This should iterate through the arrays and append all your rows to the string. Note, however, that this code does not take care of SQL Injections at all! Not to be meant for production.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top