Php 将数据库中的数据提取到复选框中,并获取所选值
我是PHP和Mysql的新手。我在Mysql中有一个名为“has”的表,用于存储客户的物理对齐。有两个属性CustomerID和PyhsicalAilmentName。在注册屏幕中,我希望用户在复选框中选择它们。我能够用这个表单代码将物理对齐从数据库提取到复选框中Php 将数据库中的数据提取到复选框中,并获取所选值,php,html,mysql,checkbox,Php,Html,Mysql,Checkbox,我是PHP和Mysql的新手。我在Mysql中有一个名为“has”的表,用于存储客户的物理对齐。有两个属性CustomerID和PyhsicalAilmentName。在注册屏幕中,我希望用户在复选框中选择它们。我能够用这个表单代码将物理对齐从数据库提取到复选框中 <form action="includes/signup.inc.php" style="border:1px solid #ccc;width: 50%;margin: 0 auto" method="post" &
<form action="includes/signup.inc.php" style="border:1px solid #ccc;width: 50%;margin: 0 auto" method="post" >
<div class="container" >
<h1>Sign up</h1>
<p>Please fill in this form to create an account.(Your username should start with "C_")</p>
<hr>
<input type="text" name="username" placeholder="Name">
<input type="text" name="user_last_name" placeholder="Last Name">
<input type="text" name="uid" placeholder="Username">
<input type="password" name="pwd" placeholder="Password">
<input type="password" name="pwd-repeat" placeholder="Repeat Password">
<input type="text" name="user_weight" placeholder="Weight(in terms of kilogram)">
<input type="text" name="user_length" placeholder="Length(in terms of cm)">
<input type="text" name="user_age" placeholder="Age">
<p> Phsical Alignments</p>
<?php
$sql = "select Name from physical_ailment";
$result = mysqli_query($conn,$sql);
$i = 0;
while($db_row = mysqli_fetch_array($result)){
?>
<input type="checkbox" name="check_list[]"> <?php
echo $db_row["Name"]; ?> <br>
<?php
$i++; }
?>
<select name="selected_mem_type" >
<?php
$sql = "select Name from membership_type";
$result = mysqli_query($conn,$sql);
$i = 0;
while($DB_ROW = mysqli_fetch_array($result)){
?>
<option>
<?php echo $DB_ROW['Name']; ?>
</option>
<?php
$i++;} ?>
</select>
<input type ="text" name="user_card_no" placeholder="Credit Card No">
<input type="date" name="user_card_expire_date" placeholder="Expire Date of Card">
<button type="submit" class="signupbtn" name="signup-submit"> Signup </button>
<button type="submit" class="signupbtn" name="home-submit"> Home </button>
</div>
</form>
注册
请填写此表格以创建帐户。(您的用户名应以“Cā”开头)
韧皮部排列
报名
家
问题是当我打算通过foreach循环使用$\u POST['check\u list']
获取所选内容时,它会根据所选复选框的数量在“上打印”。如果用户在$\u POST['check\u list']
中选择了3个复选框,则有3个元素位于“上。当我选择2件事时,比如说,用print\r
打印$\u POST['check\u list']]
它输出数组([0]=>on[1]=>on[2]=>on)
我搜索了很多,但没能找到解决方案。感谢您的帮助,谢谢您的关注。如果您在HTML中没有为复选框提供
值
属性,在大多数浏览器中,它将默认为on
,以让您知道它已被选中
所以,如果你在做复选框,要求人们检查他们最喜欢的3种水果
<input type="checkbox" name="check_list[]"> Banana <br>
<input type="checkbox" name="check_list[]"> Apple <br>
<input type="checkbox" name="check_list[]"> Orange <br>
如果所有3项都选中,您将有:
Array([0]=>banana[1]=>apple[2]=>orange)
您可以在此处了解更多信息:经过10年的内容生成和数百万个问题,大多数基本编程问题至少有5页,说明如何解决给定问题。在提出一个新问题之前,请在寻找解决方案时多努力一点。
<input type="checkbox" name="check_list[]" value="banana"> Banana <br>
<input type="checkbox" name="check_list[]" value="apple"> Apple <br>
<input type="checkbox" name="check_list[]" value="orange"> Orange <br>