我正在使用此代码:
$sortId = $_POST['sortId'];
$sortName = $_POST['sortName'];
$sortType = $_POST['sortType'];
$location = $_POST['location'];
$active = $_POST['active'];
$sql = "UPDATE `sorts` SET `sort` = :sort
WHERE `sort_id` = :sort_id";
$stmt = $db->prepare($sql);
$stmt->bindParam(':sort', $sort);
$stmt->bindParam(':sort_id', $sort_id);
$db->beginTransaction();
foreach($sortName as $i => $sort)
{
$sort_id = $sortId[$i];
$stmt->execute();
}
$db->commit();
它可以正常工作,所以我继续添加额外的值
$sortId = $_POST['sortId'];
$sortName = $_POST['sortName'];
$sortType = $_POST['sortType'];
$location = $_POST['location'];
$active = $_POST['active'];
$sql = "UPDATE `sorts` SET `sort` = :sort, `shipping_sort` = :sortType
WHERE `sort_id` = :sort_id";
$stmt = $db->prepare($sql);
$stmt->bindParam(':sort', $sort);
$stmt->bindParam(':sort_id', $sort_id);
$stmt->bindParam(':sortType', $sortType);
$db->beginTransaction();
foreach($sortName as $i => $sort)
{
$sort_id = $sortId[$i];
$stmt->execute();
}
$db->commit();
HTML
<div class="chuteAudit">
<div>
<input type="hidden" name="sortId[]" value="<?php echo $name['sort_id']; ?>" >
<input type="text" name="sortName[]" value="<?php echo $name['sort']; ?>" >
<select name="sortType[]" >
<optgroup label="<?php echo $name['shipping_sort'] ?>">
<option value="amzl[]">AMZL</option>
<option value="other[]">Other</option>
</optgroup>
</select>
<select name="location[]" >
<option value=""><?php echo $name['section'] ?></option>
<option value="chutes">Chutes</option>
<option value="spine">Spine</option>
<option value="RMspine">Royal Mail spine</option>
<option value="RMchutes">Royal Mail chutes</option>
<option value="exceptions">Exceptions</option>
<option value="teamLift">Team lift</option>
</select>
<select name="active[]" >
<option value=""><?php echo $name['active'] ?></option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
</div>
</div>
我曾经认为这样会很好,因为它只会添加到已经有效的东西上,但它并不像我原先想象的那么简单,目前我收到的错误消息Notice: Array to string conversion
我知道它与我添加的其他代码相关,但我不确定我做错了什么。
答案 0 :(得分:1)
在表单中使用sortType[]
会使您的$sortType
成为一个数组。
你在这里绑定数组:
$stmt->bindParam(':sortType', $sortType); //$sortType is an array
你需要绑定的更像是
$stmt->bindParam(':sortType', $sort_type);
然后添加到foreach
foreach($sortName as $i => $sort)
{
$sort_id = $sortId[$i];
$sort_type = $sortType[$i];
$stmt->execute();
}
类似于如何遍历sortId,遍历sortType