我正在使用PHP遍历书籍列表,并创建一个表单以将它们添加到具有不同状态(1 =已读,2 =正在阅读,3 =待读)的自己的列表中。
每本书都有一个唯一的ID。所以我最终使用foreach循环得到了这样的数组
foreach($_POST as $key => $val){
$count++;
$formElements[] = $val;
}
Array ( [0] => 6748 [1] => 1 [2] => 6759 [3] => 2 [4] => 87804 [5] => 3 [6] => [7] => submit )
因此[0]和[1]是book_ID和状态。与[2]和[3],[4]和[5]相同。
如何将它们合并在一起或将它们插入SQL表中?
显示我的表格,对不起!
<table>
<thead>
<tr><th></th>
<th>Title</th>
<th>Author</th>
</tr></thead><tbody><tr>
<td><input type="checkbox" value="6748" id="bookID" name="bookID1"></td>
<td>A Supposedly Fun Thing I'll Never Do Again: Essays and Arguments</td>
<td>David Foster Wallace</td>
<td><select name="readStatus1">
<option></option>
<option value="1">Read</option>
<option value="2">Reading</option>
<option value="3">To-Read</option>
</select></td>
</tr><tr>
<td><input type="checkbox" value="6759" id="bookID" name="bookID2"></td>
<td>Infinite Jest</td>
<td>David Foster Wallace</td>
<td><select name="readStatus2">
<option></option>
<option value="1">Read</option>
<option value="2">Reading</option>
<option value="3">To-Read</option>
</select></td>
</tr><tr>
<td><input type="checkbox" value="87804" id="bookID" name="bookID3"></td>
<td>Yes Man</td>
<td>Danny Wallace</td>
<td><select name="readStatus3">
<option></option>
<option value="1">Read</option>
<option value="2">Reading</option>
<option value="3">To-Read</option>
</select></td>
</tr><tr>
<td><input type="checkbox" value="5986375" id="bookID" name="bookID4"></td>
<td>This Is Water: Some Thoughts, Delivered on a Significant Occasion, about Living a Compassionate Life</td>
<td>David Foster Wallace</td>
<td><select name="readStatus4">
<option></option>
<option value="1">Read</option>
<option value="2">Reading</option>
<option value="3">To-Read</option>
</select></td>
</tr></tbody></table><input type="submit" value="submit" name="submit">
答案 0 :(得分:0)
我建议您在表单内执行以下操作:
<input type="text" name="bookReadStatus[bookIDHERE]">
<input type="text" name="bookReadStatus[bookIDHERE]">
然后使用这种超级简单的方法将其导入数据库的方法就是
https://www.php.net/manual/en/function.implode.php
implode(',',$_POST);
答案 1 :(得分:0)
以表格形式进行操作或转换为具有键值的数组
Array ( [0] => 6748 [1] => 1 [2] => 6759 [3] => 2 [4] => 87804 [5] => 3 [6] => [7] => submit )
// you could use foreach( $a as $v)
$ma = [
[
'id' => $a[0],
'status' => $a[1]
],
/* etc */
]
// use json_encode - use you pdo syntax here
'insert into mysql( "store field") values( "' + json_encode( $ma) +'")';
答案 2 :(得分:0)
我的建议是使books数组已经存在于html中:
<!-- here -->
<td><input type="checkbox" value="6748" id="bookID" name="bookID[]"></td>
<td>A Supposedly Fun Thing I'll Never Do Again: Essays and Arguments</td>
<td>David Foster Wallace</td>
<!-- and here: -->
<td><select name="readStatus[]">
<option></option>
<option value="1">Read</option>
<option value="2">Reading</option>
<option value="3">To-Read</option>
</select></td>
<!-- repeat for other books coming from db -->
然后,您可以访问这些数组并在php中进行迭代:
foreach($_POST['bookID'] as $index => $id) {
// use the bookID's index to get the status:
$status = $_POST['readStatus'][$index];
// now use $id and $status to insert into database.
// either directly here in the loop, or build a new dedicated array:
$books[] = ["id" => $id, "status" => $status];
// and use that later to do your inserts.
}
或者,您也可以通过将ID用作索引来使用@TimHinz方法