Php 基于数据库设置复选框
我有一个系统,用户可以选择他们的选择,一旦他们保存并返回页面,将根据数据库中保存的值选中复选框,用户可以更改/更新他们的选择。下面是我的代码:Php 基于数据库设置复选框,php,jquery,html,Php,Jquery,Html,我有一个系统,用户可以选择他们的选择,一旦他们保存并返回页面,将根据数据库中保存的值选中复选框,用户可以更改/更新他们的选择。下面是我的代码: <?php $conn=new PDO( "mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = $conn->prepar
<?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 } ?>
引擎
>
最多可更换6.0升标准机油
>
更换机油滤清器,最高20美元
>
执行发动机诊断扫描
然而,它所做的只是反复展示手风琴和它的孩子
数据的保存方式如下不要使用
foreach
;相反,使用while
循环
改变
$rows = $sql->fetchAll();
foreach ($rows as $row) { ?>
致:
我假设您在表
business\u 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>
引擎
>
最多可更换6.0升标准机油
>
更换机油滤清器,最高20美元
>
执行发动机诊断扫描
a您的数据是如何保存的?@MarceloOrigoni我已经更新了我的问题,它仍然反复显示手风琴。我应该在哪里放置while循环?因为手风琴有很多种。超过10个。在上面的代码中,我只放了一个手风琴。@fredrick用前面提到的while
语句替换所示的两行。另外,请确保SQL查询返回的值与您计算的值一致(即没有尾随空格或前导空格等)。是的,我确实替换了该值。但是如果$rows返回10个结果,则手风琴将显示为10,并且只有最后一个手风琴选中了复选框。如何避免重复显示?
<?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>