保存する必要がある入力の値が 1 回だけの場合、請求書フォームの DB に値を挿入または更新する方法を知る必要がありますか?
すべての行で製品を選択する請求書フォームがあり、最後に残りの入力とともに保存する必要がある値を持つ入力 (user_id) があります。
例のように、請求書で次を選択します。
10 tomatoes
5 garlics
2 beans
そして最後に私のIDがあります(一意のPRODUCTOSテーブルのIDではなく、user_id)
Id=1
これが私のテーブルのスキーマであり、これまでの保存または更新方法は次のとおりです。
| cantidad | nombre del producto | Id |
+------------+---------------------+-------+
| 10 | Tomatoes | 1 |
| 5 | garlics | 0 |
| 2 | beans | 0 |
ここで、保存または更新する必要があるもの:
| cantidad | nombre del producto | Id |
+------------+---------------------+-------+
| 10 | Tomatoes | 1 |
| 5 | garlics | 1 |
| 2 | beans | 1 |
コードは次のとおりです。
$conn->beginTransaction();
$sql = "INSERT INTO PRODUCTOS
(cantidad, nombreProd, Id)
VALUES ";
$insertQuery = array();
$insertData = array();
foreach ($_POST['cantidad'] as $i => $cantidad) {
$insertQuery[] = '(?, ?, ?)';
$insertData[] = $_POST['cantidad'][$i];
$insertData[] = $_POST['nombreProd'][$i];
$insertData[] = $_POST['Id'][$i];
}
if (!empty($insertQuery)) {
$sql .= implode(', ', $insertQuery);
$stmt = $conn->prepare($sql);
$stmt->execute($insertData);
}
$conn->commit();
ありがとうございました