如何在 PHP 更新表单中显示选中的复选框?后置 sql
How to display a checked checkbox in a PHP update form? postgres sql
我必须显示选中的复选框 (like this one), but currently when extracting the value from the database (it shows this)
有没有办法在从数据库中提取已知值时显示选中的复选框 (PgAdmin)(所有代码都是 HTML、CSS 或 PHP)
代码:
Topings: <br>
<br>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="Vanilla") echo "checked" ?> value="<?php echo $row['toping']; ?>">
<span class="checkmark"></span>
<span class="checkbox-txt"> Vanilla</span>
<br /></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="Chocolate") echo "checked" ?>value="<?php echo $row['toping']; ?>">
<span class="checkmark"></span>
<span class="checkbox-txt">Chocolate</span>
<br /></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="Caramel") echo "checked" ?>value="<?php echo $row['toping']; ?>">
<span class="checkmark"></span>
<span class="checkbox-txt">Caramel</span>
<br /></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="Strawberry") echo "checked" ?>value="<?php echo $row['toping']; ?>">
<span class="checkmark"></span>
<span class="checkbox-txt">Strawberry</span>
<br /></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="M&M's") echo "checked" ?>value="<?php echo $row['toping']; ?>">
<span class="checkmark"></span>
<span class="checkbox-txt">M&M's</span>
<br /></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="Oreo") echo "checked" ?>value="<?php echo $row['toping']; ?>">
<span class="checkmark"></span>
<span class="checkbox-txt">Oreo</span>
<br /></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="Meringue") echo "checked" ?>value="<?php echo $row['toping']; ?>">
<span class="checkmark "></span>
<span class="checkbox-txt">Meringue</span>
<br />
</label>
首先,您必须知道如何将多个复选框值存储到数据库中。如果您需要,我已经添加了参考资料。
假设您的 toppings
保存为 逗号分隔值,如下所示:
您可以使用 explode()
函数将 逗号分隔值 分解为平面 索引数组.
从 Chocolate,Caramel,M&M's
到 ["Chocolate", "Caramel", "M&M's"]
。
然后你可以使用in_array()
函数来检查item(topping)是否在数组.
PHP
/* Connection with PDO */
// DB details (Change it to your DB details)
$host = '127.0.0.1';
$user = 'root';
$password = 'root';
$dbname = 'test';
$port = '3306';
// DataSourceName for MySQL (The one I used for testing)
$dsn = "mysql:dbname=$dbname;host=$host;port=$port";
// DataSourceName for PostgreSQL (Use this and add port if you need)
$dsn = 'pgsql:dbname=$dbname;host=$host;';
$pdo = new PDO($dsn, $user, $password);
/* End Connection String */
// Get Toppings Value From DB
$statement = $pdo->query("SELECT `toppings` FROM toppings");
$res = $statement->fetch();
//res[0] = Comma Separated String of Toppings: 'Topping1,Topping2,Topping3'
$toppings = explode(',', $res[0]);
// $toppings = Array of toppings checked: ["Topping1", "Topping2", "Topping3" ]
/* Connection with PG */
$db = pg_connect("host=localhost port=5432 dbname=SRIS user=postgres password=password");
$result = pg_query($db, "SELECT * FROM tablename ");
$row = pg_fetch_assoc($result);
$toppings = explode(',', $row['toppings']);
HTML 表格
<label class="checkbox-container">
<input type="checkbox" name="toping[]" <?=(in_array('Vanilla',$toppings) ? 'checked="checked"' : '')?> value="Vanilla">
<span class="checkmark"></span>
<span class="checkbox-txt"> Vanilla</span><br/></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping[]" <?=(in_array('Chocolate',$toppings) ? 'checked="checked"' : '')?> value="Chocolate">
<span class="checkmark"></span>
<span class="checkbox-txt">Chocolate</span><br/></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping[]" <?=(in_array('Caramel',$toppings) ? 'checked="checked"' : '')?> value="Caramel">
<span class="checkmark"></span>
<span class="checkbox-txt">Caramel</span><br/></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping[]" <?=(in_array('Strawberry',$toppings) ? 'checked="checked"' : '')?> value="Strawberry">
<span class="checkmark"></span>
<span class="checkbox-txt">Strawberry</span><br/></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping[]" <?=(in_array("M&M's",$toppings) ? 'checked="checked"' : '')?> value="M&M's">
<span class="checkmark"></span>
<span class="checkbox-txt">M&M's</span><br/></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping[]" <?=(in_array('Oreo',$toppings) ? 'checked="checked"' : '')?> value="Oreo">
<span class="checkmark"></span>
<span class="checkbox-txt">Oreo</span><br/></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping[]" <?=(in_array('Meringue',$toppings) ? 'checked="checked"' : '')?> value="Meringue">
<span class="checkmark "></span>
<span class="checkbox-txt">Meringue</span><br/>
</label>
注:
- 我已经将 name 属性更改为
name=topping[]
,你应该使用 field name array.
- 我还更改了 value 属性,为每个复选框提供了 预定值。
参考文献:
- 爆炸:Comma-delimited string into an array?
<?=
: What does '<?=' means in PHP
- 将复选框值存储到数据库的示例:How to Insert Multiple Checkbox Value to Database
我必须显示选中的复选框 (like this one), but currently when extracting the value from the database (it shows this)
有没有办法在从数据库中提取已知值时显示选中的复选框 (PgAdmin)(所有代码都是 HTML、CSS 或 PHP)
代码:
Topings: <br>
<br>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="Vanilla") echo "checked" ?> value="<?php echo $row['toping']; ?>">
<span class="checkmark"></span>
<span class="checkbox-txt"> Vanilla</span>
<br /></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="Chocolate") echo "checked" ?>value="<?php echo $row['toping']; ?>">
<span class="checkmark"></span>
<span class="checkbox-txt">Chocolate</span>
<br /></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="Caramel") echo "checked" ?>value="<?php echo $row['toping']; ?>">
<span class="checkmark"></span>
<span class="checkbox-txt">Caramel</span>
<br /></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="Strawberry") echo "checked" ?>value="<?php echo $row['toping']; ?>">
<span class="checkmark"></span>
<span class="checkbox-txt">Strawberry</span>
<br /></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="M&M's") echo "checked" ?>value="<?php echo $row['toping']; ?>">
<span class="checkmark"></span>
<span class="checkbox-txt">M&M's</span>
<br /></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="Oreo") echo "checked" ?>value="<?php echo $row['toping']; ?>">
<span class="checkmark"></span>
<span class="checkbox-txt">Oreo</span>
<br /></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="Meringue") echo "checked" ?>value="<?php echo $row['toping']; ?>">
<span class="checkmark "></span>
<span class="checkbox-txt">Meringue</span>
<br />
</label>
首先,您必须知道如何将多个复选框值存储到数据库中。如果您需要,我已经添加了参考资料。
假设您的 toppings
保存为 逗号分隔值,如下所示:
您可以使用
explode()
函数将 逗号分隔值 分解为平面 索引数组.从
Chocolate,Caramel,M&M's
到["Chocolate", "Caramel", "M&M's"]
。然后你可以使用
in_array()
函数来检查item(topping)是否在数组.
PHP
/* Connection with PDO */
// DB details (Change it to your DB details)
$host = '127.0.0.1';
$user = 'root';
$password = 'root';
$dbname = 'test';
$port = '3306';
// DataSourceName for MySQL (The one I used for testing)
$dsn = "mysql:dbname=$dbname;host=$host;port=$port";
// DataSourceName for PostgreSQL (Use this and add port if you need)
$dsn = 'pgsql:dbname=$dbname;host=$host;';
$pdo = new PDO($dsn, $user, $password);
/* End Connection String */
// Get Toppings Value From DB
$statement = $pdo->query("SELECT `toppings` FROM toppings");
$res = $statement->fetch();
//res[0] = Comma Separated String of Toppings: 'Topping1,Topping2,Topping3'
$toppings = explode(',', $res[0]);
// $toppings = Array of toppings checked: ["Topping1", "Topping2", "Topping3" ]
/* Connection with PG */
$db = pg_connect("host=localhost port=5432 dbname=SRIS user=postgres password=password");
$result = pg_query($db, "SELECT * FROM tablename ");
$row = pg_fetch_assoc($result);
$toppings = explode(',', $row['toppings']);
HTML 表格
<label class="checkbox-container">
<input type="checkbox" name="toping[]" <?=(in_array('Vanilla',$toppings) ? 'checked="checked"' : '')?> value="Vanilla">
<span class="checkmark"></span>
<span class="checkbox-txt"> Vanilla</span><br/></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping[]" <?=(in_array('Chocolate',$toppings) ? 'checked="checked"' : '')?> value="Chocolate">
<span class="checkmark"></span>
<span class="checkbox-txt">Chocolate</span><br/></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping[]" <?=(in_array('Caramel',$toppings) ? 'checked="checked"' : '')?> value="Caramel">
<span class="checkmark"></span>
<span class="checkbox-txt">Caramel</span><br/></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping[]" <?=(in_array('Strawberry',$toppings) ? 'checked="checked"' : '')?> value="Strawberry">
<span class="checkmark"></span>
<span class="checkbox-txt">Strawberry</span><br/></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping[]" <?=(in_array("M&M's",$toppings) ? 'checked="checked"' : '')?> value="M&M's">
<span class="checkmark"></span>
<span class="checkbox-txt">M&M's</span><br/></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping[]" <?=(in_array('Oreo',$toppings) ? 'checked="checked"' : '')?> value="Oreo">
<span class="checkmark"></span>
<span class="checkbox-txt">Oreo</span><br/></br>
</label>
<label class="checkbox-container">
<input type="checkbox" name="toping[]" <?=(in_array('Meringue',$toppings) ? 'checked="checked"' : '')?> value="Meringue">
<span class="checkmark "></span>
<span class="checkbox-txt">Meringue</span><br/>
</label>
注:
- 我已经将 name 属性更改为
name=topping[]
,你应该使用 field name array. - 我还更改了 value 属性,为每个复选框提供了 预定值。
参考文献:
- 爆炸:Comma-delimited string into an array?
<?=
: What does '<?=' means in PHP- 将复选框值存储到数据库的示例:How to Insert Multiple Checkbox Value to Database