Php 将单选按钮和复选框中的数据发布到数据库中

Php 将单选按钮和复选框中的数据发布到数据库中,php,html,forms,Php,Html,Forms,我在网页上有一个表单,它有单选按钮和复选框,我试图在提交表单时将这些数据插入数据库。当我提交表单时,字段保持不变(默认条目)。所有其他条目都被插入,只是这两个条目给了我问题 表格创建: CREATE TABLE users ( gender varchar(255) NOT NULL, specialmem BOOL NOT NULL, ) ENGINE=InnoDB; 表格: 请用下面的代码替换html代码,然后重试 <div class="form-group">

我在网页上有一个表单,它有单选按钮和复选框,我试图在提交表单时将这些数据插入数据库。当我提交表单时,字段保持不变(默认条目)。所有其他条目都被插入,只是这两个条目给了我问题

表格创建:

CREATE TABLE users (
  gender varchar(255) NOT NULL,
  specialmem BOOL NOT NULL,
) ENGINE=InnoDB;
表格:


请用下面的代码替换html代码,然后重试

<div class="form-group">
    <label for="sex">Gender:</label>
    <input type="radio" name="gender" value="female" 
        <?php if(isset($gender)) { 
            if($gender='female') { 
                echo 'selected'; 
            } 
        } ?>
    >Female
    <input type="radio" name="gender" value="male" 
        <?php if(isset($gender)) { 
            if($gender='male') { 
                echo 'selected'; 
            } 
        } ?>
    >Male
</div>
<div class="form-group">
    <label for="specialmem">Are you a Special member?</label>
    <input type="checkbox" name="specialmem" value="1" class="form-control" id="specialmem" placeholder="">
    <?php 
        if(isset($_POST['specialmem'])) {
            if ($_POST['specialmem'] == '1') {
                $specialmem = $_POST['specialmem'];
            } else {
                $specialmem=0;
            }                   
        } 
    ?>
    <button type="submit" name="submitted" class="btn btn-default">Submit</button>
</div>

性别:
>男性
你是特别会员吗?
提交

用下面的代码替换html代码,然后重试

<div class="form-group">
    <label for="sex">Gender:</label>
    <input type="radio" name="gender" value="female" 
        <?php if(isset($gender)) { 
            if($gender='female') { 
                echo 'selected'; 
            } 
        } ?>
    >Female
    <input type="radio" name="gender" value="male" 
        <?php if(isset($gender)) { 
            if($gender='male') { 
                echo 'selected'; 
            } 
        } ?>
    >Male
</div>
<div class="form-group">
    <label for="specialmem">Are you a Special member?</label>
    <input type="checkbox" name="specialmem" value="1" class="form-control" id="specialmem" placeholder="">
    <?php 
        if(isset($_POST['specialmem'])) {
            if ($_POST['specialmem'] == '1') {
                $specialmem = $_POST['specialmem'];
            } else {
                $specialmem=0;
            }                   
        } 
    ?>
    <button type="submit" name="submitted" class="btn btn-default">Submit</button>
</div>

性别:
>男性
你是特别会员吗?
提交

如果选中以下复选框,您使用的复选框表单将生成$\u POST字符串“on”:
$\u POST=array([“specialmem”]=>“on”)

但是,您的数据库是一个布尔值。您需要为此执行额外的步骤,如:
if($_POST[“specialmem”]=“on”){
$specialmem=1;//要插入的值
}

另外,您尝试插入数据库的变量名为“$specialmember”,应该是“$specialmem”


您的性别单选按钮代码似乎正常。它应该可以工作。

如果选中,您使用的复选框表单将生成$\u POST字符串“on”:
$\u POST=array([“specialmem”]=>“on”)

但是,您的数据库是一个布尔值。您需要为此执行额外的步骤,如:
if($_POST[“specialmem”]=“on”){
$specialmem=1;//要插入的值
}

另外,您尝试插入数据库的变量名为“$specialmember”,应该是“$specialmem”


你的性别单选按钮代码似乎还可以。它应该可以工作。

那么执行查询的代码在哪里?仅仅写一个查询并不能执行它。谷歌如何使用PDO,有上千篇关于它的文章。有一个按钮可以提交表单。其他字段提交没有问题,只是复选框和单选按钮没有rowser不会向PHP脚本发送未选中的复选框。您需要使用
isset($\u POST['specialmem'])
要知道它是否被选中,你没有在复选框中设置值,请尝试这样做,我建议在数据库中使用1或0进行特殊检查。执行查询的代码在哪里?只编写查询不会执行查询。谷歌如何使用PDO,关于它有上千篇文章。有一个按钮可以提交表单。O提交其他字段没有问题,只是复选框和单选按钮没有。浏览器不会将未选中的复选框发送到PHP脚本。您需要使用
isset($\u POST['specialmem'])
若要知道它是否被选中,您未在复选框中设置值,请像我建议使用数据库中的1或0进行特殊检查一样尝试
<div class="form-group">
    <label for="sex">Gender:</label>
    <input type="radio" name="gender" value="female" 
        <?php if(isset($gender)) { 
            if($gender='female') { 
                echo 'selected'; 
            } 
        } ?>
    >Female
    <input type="radio" name="gender" value="male" 
        <?php if(isset($gender)) { 
            if($gender='male') { 
                echo 'selected'; 
            } 
        } ?>
    >Male
</div>
<div class="form-group">
    <label for="specialmem">Are you a Special member?</label>
    <input type="checkbox" name="specialmem" value="1" class="form-control" id="specialmem" placeholder="">
    <?php 
        if(isset($_POST['specialmem'])) {
            if ($_POST['specialmem'] == '1') {
                $specialmem = $_POST['specialmem'];
            } else {
                $specialmem=0;
            }                   
        } 
    ?>
    <button type="submit" name="submitted" class="btn btn-default">Submit</button>
</div>