Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
php.net/manual/en/function.implode.php AND/OR php.net/manual/en/function.explode.php SO is not meant for questions like this anyway. –  Vereos Oct 2 '13 at 13:08

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.