2

I have some javascript code that generates additional input fields. So, I can know how many inputs the user will insert (or I can block the number of clones...)

so I have

$form['input'][0];
$form['input'][1];
$form['input'][2];
and so on. 

First problem

Determine how many $form['input'][]

Is this correct?

for $i=0; $i<$form.length[input][]; $i++ {
 $form['input'][$i];
}

and the query

Second problem

// number of ? must change  
($sql = $db -> prepare("INSERT INTO user (something) VALUES (?, ?)"));

$user = 1;
// problem here. this must change dynamically to the number of inputs for each user.
$sql -> bind_param('iss', $user, $form['input'][0], $form['input'][1]);
$sql->execute();

Any ideas?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
daniel__
  • 11,633
  • 15
  • 64
  • 91
  • 1
    Can you show data structure. How is the data being saved. – Andrew Jackman Jun 11 '11 at 15:37
  • var formdata = $("#customForm").serializeArray(); and data: formdata, – daniel__ Jun 11 '11 at 15:39
  • Does each element in the $form['input'] array represent a row to be inserted? – Jason Rikard Jun 11 '11 at 15:43
  • Wait wait, I think I thought the answer to my question (and what you said isn't it :p). Is the user selecting fields that are on the user table in the database? So is there a limit to the number of fields that can be added? – Andrew Jackman Jun 11 '11 at 15:44
  • I found a related question: http://stackoverflow.com/questions/793471/use-one-bind-param-with-variable-number-of-input-vars – daniel__ Jun 11 '11 at 15:45
  • "INSERT INTO user (something) VALUES (?, ?) <-that will not work you only stipulate one field (something) not (something, something_else) – Cups Jun 11 '11 at 16:01
  • i am thinking to limit the number of cloned fields, and insert null if the input is none. – daniel__ Jun 11 '11 at 16:26
  • @wire: if the number of input fields is variable and they are all the same type of data (like test scores) you need two database tables and a many-to-one relationship – Erik Jun 11 '11 at 16:51

1 Answers1

0

Determine how many $form['input']: count($form['input']);.

patapizza
  • 2,398
  • 15
  • 14