如何使用Laravel插入多个输入广播数据?
当我选择一个单选按钮并单击提交表单以插入到我的数据库中时
这是我的html:
struct Swap
{
int a = 10; // default member initialization
int b = 20; // default member initialization
Swap(int a = 20, int b = 10): a(b), b(a) {}; // swap on initialization
// using member initializer list
};
Swap s;
std::cout << s.a // 20
<< s.b // 10
<< std::endl;
我的QuestionController.php:
def graph_e2j(var_list1):
e2j = tf.Graph()
saver1 = tf.train.Saver(var_list1)
with e2j.as_default() as e2j:
saver1.restore(sess, model1)
return g.as_graph_def()
但是它只能插入到数据库的一行,而我想这样向数据库表插入数据:
<table class="table table-bordered table-condensed table-hover">
<thead>
<tr>
<th bgcolor="#DFF0D8">
<h5>
<b>
<center>Question No</center>
</b>
</h5>
</th>
<th bgcolor="#DFF0D8">
<h5>
<b>
<center>Yes</center>
</b>
</h5>
</th>
<th bgcolor="#DFF0D8">
<h5>
<b>
<center>No</center>
</b>
</h5>
</th>
</tr>
</thead>
<tbody>
<tr>
<td id="question1">
<h5>1.You Like It's
</h5>
</td>
<!-- true checkbox Q1 -->
<td>
<div class="flat-green single-row">
<div class="radio ">
<center>
<input name="q1[]" type="radio" value="1">
</center>
</div>
</div>
</td>
<td>
<div class="flat-green single-row">
<div class="radio ">
<center>
<input name="q1[]" type="radio" value="0">
</center>
</div>
</div>
</td>
</tr>
<tr>
<td id="question2">
<h5>2.you don't like it's
</h5>
</td>
<!-- true checkbox Q2 -->
<td>
<div class="flat-green single-row">
<div class="radio ">
<center>
<input name="q2[]" type="radio" value="1">
</center>
</div>
</div>
</td>
<td>
<div class="flat-green single-row">
<div class="radio ">
<center>
<input name="q2[]" type="radio" value="0">
</center>
</div>
</div>
</td>
</tr>
</tbody>
</table>
如何在数据库表中插入多个数据?
答案 0 :(得分:0)
多个数据意味着您必须插入多次。 $request->get('q1')
和$request->get('q2')
都产生数组,因此您必须遍历它们(或array_sum()
,或者将q1 / 2值汇总到单个score
所需执行的所有操作柱)。
现在,假设score
是检查值的总和,而id
是一个自动增量列,则看起来像这样:
// deal with the q1 array
$q1_array = $request->input('q1');
$question = new Question;
foreach ($q1_array as $q1_answer) {
// i'm assuming table.score is the sum of the values of the arrays
$q1_total =+ $q1_answer;
}
$question->question_no = 'question1';
$question->score = $q1_total;
$question->save();
// now deal with the q2 array
$q2_array = $request->input('q2');
$question = new Question;
foreach ($q2_array as $q2_answer) {
// i'm assuming table.score is the sum of the values of the arrays
$q2_total =+ $q2_answer;
}
$question->question_no = 'question2';
$question->score = $q2_total;
$question->save();
当然,这只是为了清楚起见。您可能在外面有一个单独的循环,所以您不必重复问题1和问题2的代码。
这里是一个更缩写的版本,假设您的表名为answers
:
DB::table('answers')->insert([
['user_id' => $userId, 'question_no' => 'question1', 'score' => array_sum($request->input('q1'))],
['user_id' => $userId, 'question_no' => 'question2', 'score' => array_sum($request->input('q2'))],
]);