Web Analytics Made Easy -
StatCounter Multiple insert statement - CodingForum

Announcement

Collapse
No announcement yet.

Multiple insert statement

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple insert statement

    I have a form with a series of questions on it (20 in total)

    Each question has a value which is the answer.

    I would like to insert 20 rows into the table - the question id and answer.

    The question id isn't being passed through the form but it will start at 1 and end at 20.

    How can I insert multiple rows with the question id and answer that is passed through?

    Thanks

  • #2
    Give your form elements these names:

    <textarea name="answer[0]">...</textarea>

    Where 0 is the first Question-ID. Counting up to 20, in your case.

    So you'll have an array $_POST['answer'] on the next page.

    PHP Code:
    $answer $_POST['answer'];
    foreach(
    $answer as $q_id => $q_answer)
    {
        
    $sql "INSERT INTO answer_table (question_id, answer) VALUES ('$q_id', '$q_answer')";
        
    $res = (mysql_query($sql) || false);
    }

    if(!
    $res)echo "At least one INSERT failed!";
    else 
    "All INSERTs successfully executed."
    <not tested>

    Saludo
    piz
    www.united-scripts.com
    www.codebattles.org

    Comment

    Working...
    X