简体   繁体   English

复选框值未传递到数据库

[英]Check Box value not being passed on to database

I am using an MySQL table with the following structure: 我正在使用具有以下结构的MySQL表:

`login` (
  `loginid` int(11) unsigned NOT NULL auto_increment,
  `username` varchar(30) NOT NULL,
  `password` varchar(50) NOT NULL,
  `email` varchar(255) NOT NULL,
  `actcode` varchar(45) NOT NULL,
  `disabled` tinyint(1) NOT NULL default '0',
  `activated` tinyint(1) NOT NULL default '0',
  `created` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `points` bigint(9) NOT NULL,
  `website` varchar(1000) NOT NULL,
  `location` varchar(1000) NOT NULL,
  `age` varchar(1000) NOT NULL,
  `gender` varchar(1000) NOT NULL,
  `subcheckr` tinyint(1) NOT NULL,
  PRIMARY KEY  (`loginid`)
)

I have several fields that the user can fill out while registering. 我有几个字段,用户可以在注册时填写。 They all work fine (the information is transmitted to the database). 它们都工作正常(信息已传输到数据库)。 I also have a checkbox for a user to select while registering. 我也有一个复选框供用户在注册时选择。 However, when a user selects the check box, the value "1" is not being transmitted to the database. 但是,当用户选中该复选框时,值“ 1”不会传输到数据库。 The checkbox has the name of "subcheckr" in both the code below and the database. 在下面的代码和数据库中,该复选框的名称均为“ subcheckr”。

Any idea why the checkbox is not working? 知道为什么复选框不起作用吗?

Thanks in advance, 提前致谢,

John 约翰

When a user registers, I am using the following code: 用户注册时,我正在使用以下代码:

function show_registration_form(){

    echo '<form action="http://www...com/.../register.php" method="post">  

    <div class="register"><label for="username">Select Username:</label></div> 
    <div class="registerform"><input name="username" type="text" id="username" maxlength="30"></div>

    <div class="passwordregister"><label for="password">Select Password:</label></div> 
    <div class="passwordregisterform"><input name="password" type="password" id="password" maxlength="15"></div>

    <div class="passwordregister2"><label for="password2">Re-type password:</label></div> 
    <div class="passwordregister2form"><input name="password2" type="password" id="password2" maxlength="15"></div>

    <div class="emailregister"><label for="email">Your Email (required):</label></div> 
    <div class="emailregisterform"><input name="email" type="text" id="email" maxlength="255"></div> 


    <div class="websiteregister"><label for="website">Your Website (optional):</label></div> 
    <div class="websiteregisterform"><input name="website" type="text" id="website" maxlength="255"></div> 

    <div class="locationregister"><label for="website">Your Location (optional):</label></div> 
    <div class="locationregisterform"><input name="location" type="text" id="location" maxlength="255"></div>

    <div class="ageregister"><label for="website">Your Age (optional):</label></div> 
    <div class="ageregisterform"><input name="age" type="text" id="age" maxlength="255"></div>

    <div class="genderregister"><label for="website">Your Gender (optional):</label></div> 
    <div class="genderregisterform"><input name="gender" type="text" id="gender" maxlength="255"></div>

    <div class="subcheckr"><INPUT TYPE=CHECKBOX NAME="subcheckr">Click here to receive updates via email (optional).<P></div>

    <p class="registerbutton"> 
    <input name="register" type="submit" value="Register"> 
    </p> 

    </form>';

}

The following code is on the page register.php: 以下代码位于register.php页面上:

if (isset($_POST['register'])){

    if (registerNewUser($_POST['username'], $_POST['password'], $_POST['password2'], $_POST['email'], $_POST['website'], $_POST['location'], $_POST['age'], $_POST['gender'], $_POST['subcheckr'])){


        echo '<div class="submittitle">Thank you for registering, an email has been sent to your inbox, Please activate your account.</div>';


    }else {

        echo '<div class="submittitler">There was a problem with your registration.  Perhaps the username or email you picked was already in use.  Please try again.</div>';
        show_registration_form();

    }

} else {
// has not pressed the register button
    show_registration_form();   
}

Here is the new registration function: 这是新的注册功能:

function registerNewUser($username, $password, $password2, $email, $website, $location, $age, $gender, $subcheckr)
{

    global $seed;

    if (!valid_username($username) || !valid_password($password) || 
            !valid_email($email) || $password != $password2 || user_exists($username) || email_exists($email))
    {
        return false;
    }


    $code = generate_code(20);
    $sql = sprintf("insert into login (username,password,email,actcode, website, location, age, gender, subcheckr) value ('%s','%s','%s','%s','%s','%s','%s','%s','%s')",
        mysql_real_escape_string($username), mysql_real_escape_string(sha1($password . $seed))
        , mysql_real_escape_string($email), mysql_real_escape_string($code), mysql_real_escape_string($website), mysql_real_escape_string($location),               mysql_real_escape_string($age), mysql_real_escape_string($gender), mysql_real_escape_string($subcheckr));


    if (mysql_query($sql))
    {
        $id = mysql_insert_id();

        if (sendActivationEmail($username, $password, $id, $email, $code))
        {

            return true;
        } else
        {
            return false;
        }

    } else
    {
        return false;
    }
    return false;

}

replace this line: 替换此行:

<INPUT TYPE=CHECKBOX NAME="subcheckr">

with: 与:

<INPUT TYPE=CHECKBOX NAME="subcheckr" value="1">

The easiest thing I do with checkboxes is rather than pull the value I just check whether they have been set and then put a value accordingly: 我使用复选框最简单的方法是提取值,而不是仅检查它们是否已设置,然后相应地输入值:

$checkboxDatabaseValue = (isset($_POST["checkboxname"]) ? 1 : 0);

Works always for me. 一直为我工作。

However, when a user selects the check box, the value "1" is not being transmitted to the database 但是,当用户选中该复选框时,值“ 1”未传输到数据库

Your check box has no value attribute, does it? 您的复选框没有value属性,对吗?

<INPUT TYPE=CHECKBOX NAME="subcheckr">

It doesn't look like you specified a value for the subcheckr checkbox in your HTML. 您似乎没有在HTML中为subcheckr复选框指定值。

I think HTML checkboxes return "on" or "off" if you don't specify a value. 我认为如果不指定值,HTML复选框将返回“ on”或“ off”。 You should change that line to: 您应该将该行更改为:

<INPUT TYPE=CHECKBOX NAME="subcheckr" value="1">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM