英文:
Insert multiple selected rows checkbox values to database In Laravel
问题
I have a table that has three rows retrieved from SQL with foreach. I want to select two rows and insert them into another table in SQL.
The problem is that the code I wrote does not take the selected rows but the first two rows and inserts them into SQL.
英文:
I have a table that has three rows retrieved from SQL with foreach. I want to select two rows and insert them into another table in SQL.
The problem is that the code I wrote does not take the selected rows but the first two rows and inserts them into SQL.
<table id="myTable" class="table table-hover">
<thead>
<tr>
<th width="1%">#</th>
<th width="10%">Name product</th>
<th width="40%">Price<span class="text-danger font-weight-bold">*</span></th>
<th width="6%">Status <span class="text-danger font-weight-bold">*</span></th>
<th width="1%"></th>
</tr>
</thead>
<tbody>
@php($count = 0)
@foreach($product as $row)
@php($count++)
<tr>
<td><span>{{$count}}</span></td>
<td><input type="text" name="product_name[]" value="{{ $row->product_name }}" class="form-control" readonly></td>
<td><input type="number" name="product_rate[]" value="{{ $row->product_rate }}" class="form-control" readonly></td>
<td><input type="text" name="product_status[]" value="{{ $row->product_status }}" class="form-control" readonly></td>
<td><input type="checkbox" name="selectedRows[]" value="{{$count}}"></td>
</tr>
@endforeach
</tbody>
</table>
<?php
public function newProductTable(Request $request)
{
$selectedRows = $request->input('selectedRows', []);
if (!$selectedRows) {
return redirect()->back()->with('error', 'Error');
} else {
$current_date = date('Y-m-d H:i:s');
{
$selectedRows = $request->input('selectedRows', []);
foreach ($selectedRows as $count) {
DB::table('new_product_table')->insert([
'product_name' => $request->product_name[$count],
'product_rate' => $request->product_rate[$count],
'product_status' => $request->product_status[$count],
'created_at' => $current_date,
'updated_at' => $current_date,
]);
}
}
return redirect()->back()->with('success', 'Successfully');
}
}
?>
答案1
得分: 1
将复选框的值设置为标识符,即每行的ID。
<td><input type="checkbox" name="selectedRows[]" value="{{ $row->id }}"></td>
更新后的代码
$selectedRows = $request->input('selectedRows', []);
foreach ($selectedRows as $productId) {
$product = DB::table('original_table')->where('id', $productId)->first();
DB::table('new_product_table')->insert([
'product_name' => $product->product_name,
'product_rate' => $product->product_rate,
'product_status' => $product->product_status,
'created_at' => $current_date,
'updated_at' => $current_date,
]);
}
将'original_table'
替换为从中检索所选行的适当表名。
英文:
Set the value of the checkboxes to the identifier, in this case, the id of each row.
<td><input type="checkbox" name="selectedRows[]" value="{{ $row->id }}"></td>
Updated code
$selectedRows = $request->input('selectedRows', []);
foreach ($selectedRows as $productId)
{
$product = DB::table('original_table')->where('id', $productId)->first();
DB::table('new_product_table')->insert([
'product_name' => $product->product_name,
'product_rate' => $product->product_rate,
'product_status' => $product->product_status,
'created_at' => $current_date,
'updated_at' => $current_date,
]);
}
Substitute 'original_table' with the appropriate table name from which you are retrieving the selected rows.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论