【发布时间】:2021-09-08 11:04:02
【问题描述】:
当我选择另一个与更新表单中选中的不同的收音机时,我无法更改数据库内一行中的值...总是返回以前的状态...我怎样才能使它工作?
以下是截图:
- 如何检查无线电
- 成功消息
但是当我刷新窗口时,选中的输入总是前一个。
代码如下:
//form:
.....
<td>
<input type="text" value="<?php echo $venta; ?>" name="venta[]" id="venta_<?php echo $i; ?>" class="form-control" autocomplete="off" placeholder="Precio de venta" required="required"/>
</td>
<td>
<input type="radio" name="principal[]" id="principal_<?php echo $i; ?>" <?php if($principal == 1){ echo 'value="0" checked="checked"';} else { echo 'value="1"';}; ?>>
</td>
.....
</tr>
//the insert/update code:
$sql = "INSERT INTO MEDIDAS (idMed, principal, cod, presentacion, cantidad, compra, venta, id_user)
VALUES ";
$insertQuery = array();
$insertData = array();
foreach ($_POST['venta'] as $i => $venta) {
$insertQuery[] = '(?, ?, ?, ?, ?, ?, ?, ?)';
$insertData[] = $_POST['idMed'][$i] == '' ? null : $_POST['idMed'][$i];
$insertData[] = $_POST['principal'][$i] == '' ? 0 : $_POST['principal'][$i]; //this is the row with the input radio
$insertData[] = $code;
$insertData[] = $_POST['presentacion'][$i];
$insertData[] = $_POST['existencia1'][$i];
$insertData[] = $_POST['compra'][$i];
$insertData[] = $_POST['venta'][$i];
$insertData[] = $id_user;
}
if (!empty($insertQuery)) {
$sql .= implode(', ', $insertQuery);
$sql .= " ON DUPLICATE KEY UPDATE
principal = VALUES (principal), cod = VALUES (cod), presentacion = VALUES (presentacion), cantidad = VALUES (cantidad),
compra = VALUES (compra), venta = VALUES (venta), id_user = VALUES (id_user)
";
$stmt = $conn->prepare($sql);
$stmt->execute($insertData);
}
【问题讨论】:
标签: mysql arrays for-loop foreach multiple-insert