Insert data from multiple array form inputs using php and mysql

hany

i have three fields

<textarea rows="2" name="answer[]" ></textarea>
<select name="fraction[]">...
<textarea rows="2" name="feedback[]"></textarea>

the user should fill this fields more than one time at least four times then i use php to loop through this fields to insert it in database

$answer = isset($_POST['answer']) ? $_POST['answer'] : "" ;
$fraction = isset($_POST['fraction']) ? $_POST['fraction'] : "" ;
$feedback = isset($_POST['feedback']) ? $_POST['feedback'] : "" ;

foreach($answer as $key=>$value){
    $answer = $value;
    $fraction = $fraction[$key];
    $feedback = $feedback[$key];
    $query = "insert into `question_answer` ( answer, fraction, feedback) values ('$answer', '$fraction','$feedback')";
    $questions->insertData($query,$con);
}

this insert number of records , the first record contain all values as i want but the other records only contain the value of the field related to the array i loop through and the other fields are empty..any help ??

Simon Kraus

You overwrite your variables in the first loop... Take this:

$answer = isset($_POST['answer']) ? $_POST['answer'] : "" ;
$fraction = isset($_POST['fraction']) ? $_POST['fraction'] : "" ;
$feedback = isset($_POST['feedback']) ? $_POST['feedback'] : "" ;

foreach($answer as $key=>$value){
    $query = "insert into `question_answer` ( answer, fraction, feedback) values ('$value', '$fraction[$key]','$feedback[$key]')";
    $questions->insertData($query,$con);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

storing data from multiple select form to MYSQL using PHP

From Dev

Request data from form with multiple inputs using the same name

From Dev

insert data from JSON into mysql using php

From Dev

Insert data from Javascript to MySQL using PHP

From Dev

Insert into multiple tables from one form (mysql and PHP)

From Dev

insert data to mysql database from multiple select list (html form)

From Dev

Can't insert data from a form, to MySQL; with PHP

From Dev

From Array with Multiple Inputs to if statement with varnames PHP

From Dev

Push data from multiple inputs to state array

From Dev

How to Insert automated-fill data from the form using php

From Dev

Insert array data in php/mysql

From Dev

PHP/SQL Insert data from form and insert data from another table using prepared statement

From Dev

Get inputs from multiple textfields to array and insert to db field

From Dev

MySQL PHP insert data from two rows into multidimensional array

From Dev

How to insert multiple inputs into the database using the power of PHP?

From Dev

PHP insert data using mysql

From Dev

PHP insert data using mysql

From Dev

MySQL/PHP: Insert form array into database?

From Dev

insert mutiple php form array into mysql rows

From Dev

Retrieve data from mysql database and insert it in a table using php

From Dev

php: how to insert large form data into mysql

From Dev

Insert data into multiple tables using one form

From Dev

How to send multiple inputs from form to email via php script

From Dev

html form inputs array PHP

From Dev

How to retrieve data from multiple tables using a PHP form?

From Dev

How to retrieve data from multiple tables using a PHP form?

From Dev

PHP MySQL Insert info into database from a form

From Dev

Extracting data from HTML form with inputs with JavaScript or AJAX and then passing it on to PHP

From Dev

insert data into php table using form

Related Related

  1. 1

    storing data from multiple select form to MYSQL using PHP

  2. 2

    Request data from form with multiple inputs using the same name

  3. 3

    insert data from JSON into mysql using php

  4. 4

    Insert data from Javascript to MySQL using PHP

  5. 5

    Insert into multiple tables from one form (mysql and PHP)

  6. 6

    insert data to mysql database from multiple select list (html form)

  7. 7

    Can't insert data from a form, to MySQL; with PHP

  8. 8

    From Array with Multiple Inputs to if statement with varnames PHP

  9. 9

    Push data from multiple inputs to state array

  10. 10

    How to Insert automated-fill data from the form using php

  11. 11

    Insert array data in php/mysql

  12. 12

    PHP/SQL Insert data from form and insert data from another table using prepared statement

  13. 13

    Get inputs from multiple textfields to array and insert to db field

  14. 14

    MySQL PHP insert data from two rows into multidimensional array

  15. 15

    How to insert multiple inputs into the database using the power of PHP?

  16. 16

    PHP insert data using mysql

  17. 17

    PHP insert data using mysql

  18. 18

    MySQL/PHP: Insert form array into database?

  19. 19

    insert mutiple php form array into mysql rows

  20. 20

    Retrieve data from mysql database and insert it in a table using php

  21. 21

    php: how to insert large form data into mysql

  22. 22

    Insert data into multiple tables using one form

  23. 23

    How to send multiple inputs from form to email via php script

  24. 24

    html form inputs array PHP

  25. 25

    How to retrieve data from multiple tables using a PHP form?

  26. 26

    How to retrieve data from multiple tables using a PHP form?

  27. 27

    PHP MySQL Insert info into database from a form

  28. 28

    Extracting data from HTML form with inputs with JavaScript or AJAX and then passing it on to PHP

  29. 29

    insert data into php table using form

HotTag

Archive