简体   繁体   English

使用php和mysql从多个数组形式输入中插入数据

[英]Insert data from multiple array form inputs using php and mysql

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 用户应至少一次填充此字段至少四次,然后使用php遍历此字段以将其插入数据库

$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 ?? 此插入记录数,第一条记录包含我想要的所有值,但其他记录仅包含与我循环通过的数组相关的字段的值,其他字段为空。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM