使用php和mysql从多个数组表单输入数据 [英] Insert data from multiple array form inputs using php and mysql

查看:81
本文介绍了使用php和mysql从多个数组表单输入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个字段

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

用户应该填写多于一次的字段至少四次
然后我使用php来循环这些字段以将其插入数据库中。

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

此插入记录数,第一条记录包含所有值,其他记录只包含与数组i循环相关的字段的值,其他字段为空..任何帮助??

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

这篇关于使用php和mysql从多个数组表单输入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆