复选框值未传递到数据库

时间:2010-10-05 21:00:41

标签: php mysql

我正在使用具有以下结构的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`)
)

我有几个字段,用户可以在注册时填写。它们都工作正常(信息传输到数据库)。我还有一个复选框供用户在注册时选择。但是,当用户选中复选框时,值“1”不会传输到数据库。该复选框在下面的代码和数据库中都有“subcheckr”的名称。

知道为什么复选框无效?

提前致谢,

约翰

当用户注册时,我使用以下代码:

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>';

}

以下代码位于页面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();   
}

这是新的注册功能:

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;

}

4 个答案:

答案 0 :(得分:2)

替换此行:

<INPUT TYPE=CHECKBOX NAME="subcheckr">

使用:

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

答案 1 :(得分:1)

我使用复选框最简单的方法是拉取值,我只是检查它们是否已经设置,然后相应地设置一个值:

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

总是为我工作。

答案 2 :(得分:0)

  

但是,当用户选中复选框时,值“1”不会传输到数据库

您的复选框没有value属性,是吗?

<INPUT TYPE=CHECKBOX NAME="subcheckr">

答案 3 :(得分:0)

看起来您没有为HTML中的subcheckr复选框指定值。

如果您没有指定值,我认为HTML复选框会返回“on”或“off”。您应该将该行更改为:

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