title: php insert multiple values into separate rows in mysql table
body: <p>I have a PHP form with various types of input fields (checkbox, drop-down, radio, auto-complete, etc.) What I would like to do is get users input(which might be more than one value, for instance, for question: which is your favourite movie? he might write more than 1 movie) and store these values in separate rows in Mysql table.
  here are tables in my database: </p>
  
  <p><strong>Answer:</strong> <strong>Id, userId, questionId, answer</strong></p>
  
  <p><strong>member: userId, name, family name etc.</strong></p>
  
  <p><strong>qustion: questionId, questionText</strong></p>
  
  <p>What I can do is something like this:(Note: in this case the design would be different, here I have a separate field for each question)</p>
  
  <pre><code>$stmt = $conn-&gt;prepare('UPDATE test SET q1 = :q1, q2 = :q2, q3 = :q3, q4 = :q4, q5 = :q5, q6     = :q6, q7 = :q7 WHERE username = :username');
  $stmt-&gt;execute(array(':q1' =&gt; $q1,':q2' =&gt; $q2, ':q3' =&gt; $q3, ':q4' =&gt; $q4, ':q5' =&gt; $q5, ':q    6' =&gt; $q6, ':q7' =&gt; $q7, ':username' =&gt; $_SESSION['SESS_USERNAME']));
  </code></pre>
  
  <p>My problem with this approach was that multiple value answers were inserted in the same row with a comma separated them(for instance if user wrote "Tom cruise" and "Brad Pitt", it stored both these names in the same row)</p>
  
  <p>I thought I have to replace it with something like this:</p>
  
  <pre><code>$stmt = $conn-&gt;prepare('INSERT INTO Answer (qId, answer) VALUES (:qId, :answer)');
  </code></pre>
  
  <p>but here I don't know how to define qId? (how it can recognize that each answer is related to which question)</p>
  
  <h2>EDIT:</h2>
  
  <p>In the code below, I can fetch questions form database and store it in array, but still Iam not sure how to complete this code.. should I write insert statement inside the loop (the loop which get questions from DB, so for each question, it insert qId and the answer user has inserted... am I right ??)</p>
  
  <pre><code>$arr = array(); 
  $sql="select qId from question"; 
  $result = mysql_query($sql) or die(mysql_error());
  while( $row = mysql_fetch_assoc( $result ) ) {
     arr[] = $row[ 'qId' ]; 
  } 
  </code></pre>
  
  <p>Could someone kindly help me to know how I can insert these values into multiple rows?</p>
  
  <p>Many thanks,</p>