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:
Answer: Id, userId, questionId, answer
member: userId, name, family name etc.
qustion: questionId, questionText
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)
$stmt = $conn->prepare('UPDATE test SET q1 = :q1, q2 = :q2, q3 = :q3, q4 = :q4, q5 = :q5, q6 = :q6, q7 = :q7 WHERE username = :username');
$stmt->execute(array(':q1' => $q1,':q2' => $q2, ':q3' => $q3, ':q4' => $q4, ':q5' => $q5, ':q 6' => $q6, ':q7' => $q7, ':username' => $_SESSION['SESS_USERNAME']));
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)
I thought I have to replace it with something like this:
$stmt = $conn->prepare('INSERT INTO Answer (qId, answer) VALUES (:qId, :answer)');
but here I don't know how to define qId? (how it can recognize that each answer is related to which question)
EDIT:
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 ??)
$arr = array();
$sql="select qId from question";
$result = mysql_query($sql) or die(mysql_error());
while( $row = mysql_fetch_assoc( $result ) ) {
arr[] = $row[ 'qId' ];
}
Could someone kindly help me to know how I can insert these values into multiple rows?
Many thanks,