我有一个系统,用户可以选择他们的选择,一旦他们保存并返回页面,将根据数据库中保存的值检查复选框,用户可以更改/更新他们的选择。以下是我的代码:
<?php $conn=new PDO( "mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = $conn->prepare("SELECT bs_services FROM business_service WHERE bs_center = :email AND bs_package = 'Essential'");
$sql->bindParam(':email', $email, PDO::PARAM_STR);
$sql->execute();
$rows = $sql->fetchAll();
foreach ($rows as $row) { ?>
<button type="button" class="accordion"><b>Engine</b>
</button>
<div class="panel-accordion" style="max-height: auto;">
<div class="row">
<li class="checkbox">
<input type="checkbox" id="amenity-1" name="service[]" value="Up to 4.5 L standard oil change" <?php if($row[ 'bs_services']=="Up to 4.5 L standard oil change" ) echo "checked"; ?>>
<label for="amenity-1">Up to 4.5 L standard oil change</label>
</li>
<li class="checkbox">
<input type="checkbox" id="amenity-2" name="service[]" value="Up to 6.0 L standard oil change" <?php if($row[ 'bs_services']=="Up to 6.0 L standard oil change" ) echo "checked"; ?>>
<label for="amenity-2">Up to 6.0 L standard oil change</label>
</li>
<li class="checkbox">
<input type="checkbox" id="amenity-3" name="service[]" value="Up to 6.0 L high quality change" <?php if($row[ 'bs_services']=="Up to 6.0 L high quality change" ) echo "checked"; ?>>
<label for="amenity-3">Up to 6.0 L high quality change</label>
</li>
<li class="checkbox">
<input type="checkbox" id="amenity-4" name="service[]" value="Replace oil filter up to $20" <?php if($row[ 'bs_services']=="Replace oil filter up to $20" ) echo "checked"; ?>>
<label for="amenity-4">Replace oil filter up to $20</label>
</li>
<li class="checkbox">
<input type="checkbox" id="amenity-5" name="service[]" value="Replace oil filter up to $40" <?php if($row[ 'bs_services']=="Replace oil filter up to $40" ) echo "checked"; ?>>
<label for="amenity-5">Replace oil filter up to $40</label>
</li>
<li class="checkbox">
<input type="checkbox" id="amenity-6" name="service[]" value="Carry out engine diagnotistic scan" <?php if($row[ 'bs_services']=="Carry out engine diagnotistic scan" ) echo "checked"; ?>>
<label for="amenity-6">Carry out engine diagnotistic scan</label>
</li>
</div>
<?php } ?>
然而,它所做的只是重复显示手风琴及其小孩。
数据保存为this
答案 0 :(得分:1)
我假设您保存在表business_service
每行1次检查,所以现在,您的代码打印所有复选框的次数与选择的项目一样多,您应该将复选框存储在一个数组中,然后检查数组
<?php
$conn=new PDO( "mysql:host=$servername;dbname=$dbname",
$username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql
$conn->prepare("SELECT bs_services FROM business_service WHERE bs_center = :email AND bs_package = 'Essential'"); $sql->bindParam(':email', $email, PDO::PARAM_STR); $sql->execute(); $rows = $sql->fetchAll();
foreach ($rows as $row) {
$checks[$row[ 'bs_services']] = true;
}
?>
<button type="button" class="accordion"><b>Engine</b>
</button>
<div class="panel-accordion" style="max-height: auto;">
<div class="row">
<li class="checkbox">
<input type="checkbox" id="amenity-1" name="service[]" value="Up to 4.5 L standard oil change" <?php if(isset($checks["Up to 4.5 L standard oil change"])) echo "checked"; ?>>
<label for="amenity-1">Up to 4.5 L standard oil change</label>
</li>
<li class="checkbox">
<input type="checkbox" id="amenity-2" name="service[]" value="Up to 6.0 L standard oil change" <?php if(isset($checks["Up to 6.0 L standard oil change"])) echo "checked"; ?>>
<label for="amenity-2">Up to 6.0 L standard oil change</label>
</li>
<li class="checkbox">
<input type="checkbox" id="amenity-3" name="service[]" value="Up to 6.0 L high quality change" <?php if(isset($checks["Up to 6.0 L high quality change"])) echo "checked"; ?>>
<label for="amenity-3">Up to 6.0 L high quality change</label>
</li>
<li class="checkbox">
<input type="checkbox" id="amenity-4" name="service[]" value="Replace oil filter up to $20" <?php if(isset($checks["Replace oil filter up to $20"])) echo "checked"; ?>>
<label for="amenity-4">Replace oil filter up to $20</label>
</li>
<li class="checkbox">
<input type="checkbox" id="amenity-5" name="service[]" value="Replace oil filter up to $40" <?php if(isset($checks["Replace oil filter up to $40"])) echo "checked"; ?>>
<label for="amenity-5">Replace oil filter up to $40</label>
</li>
<li class="checkbox">
<input type="checkbox" id="amenity-6" name="service[]" value="Carry out engine diagnotistic scan" <?php if(isset($checks["Carry out engine diagnotistic scan"])) echo "checked"; ?>>
<label for="amenity-6">Carry out engine diagnotistic scan</label>
</li>
</div>
答案 1 :(得分:0)
不要使用foreach
;相反,使用while
循环。
更改
$rows = $sql->fetchAll();
foreach ($rows as $row) { ?>
为:
while ($rows = $sql->fetch(PDO::FETCH_ASSOC)) {