尝试将复选框值作为输入到数据库的布尔值来处理

时间:2014-02-27 12:44:02

标签: php html html5 checkbox

尝试将复选框中的输入作为布尔值处理,以便我可以将值等输入到数据库中。值为“mailingList”,我认为我已经破解了它,但它现在只是在我的“catch”中返回一个预先定义的错误,该错误应该是无关的。下面是表单中的$ _Post

<?php
  if (isset($_POST['register'])) {
     $email = trim($_POST['email']);
     $password = trim($_POST['pwd']);
     $retyped = trim($_POST['conf_pwd']);
     $firstname = trim($_POST['fname']);
     $lastname = trim($_POST['lname']);
     $company = trim($_POST['company']);
     $mailinglist = trim($_POST['mailingListCheckbox']);
     require_once('./includes/register_user_pdo.inc.php');
  }
?>

然后是相关的register_user_pdo.inc.php

<?php
  require_once('./classes/CheckPassword.php');
  $errors = array();
  if (preg_match('/\s/', $email)) {
     $errors[] = 'Email should not contain spaces.';
  }
  if (!isset($mailingList)) {
    $mailingListValue = FALSE;
}
else {
    $mailingListValue = TRUE;
}
  $checkPwd = new Ps2_CheckPassword($password, 10);
  $checkPwd->requireMixedCase();
  $checkPwd->requireNumbers(2);
  $checkPwd->requireSymbols();
  $passwordOK = $checkPwd->check();
  if (!$passwordOK) {
     $errors = array_merge($errors, $checkPwd->getErrors());
  }
  if ($password != $retyped) {
     $errors[] = "Your passwords don't match.";
  }
  if (!$errors) {
     // include the connection file
     require_once('./includes/connection.inc.php');
     $conn = dbConnect();
     // create a salt using the current timestamp
     $salt = time();
     // encrypt the password and salt with SHA1
     $pwd = sha1($password . $salt);
     // prepare SQL statement
     $sql = 'INSERT INTO users (email, salt, pwd, lastName, firstName, company, mailingList)
     VALUES (:email, :salt, :pwd, :lastName, :firstName, :company, :mailingList)';
     $stmt = $conn->prepare($sql);
     // bind parameters and insert the details into the database
     $stmt->bindParam(':email', $email, PDO::PARAM_STR);
     $stmt->bindParam(':salt', $salt, PDO::PARAM_INT);
     $stmt->bindParam(':pwd', $pwd, PDO::PARAM_STR);
     $stmt->bindParam(':lastName', $lname, PDO::PARAM_STR);
     $stmt->bindParam(':firstName', $fname, PDO::PARAM_STR);
     $stmt->bindParam(':company', $company, PDO::PARAM_STR);
     $stmt->bindParam(':mailingList', $mailingListValue, PDO::PARAM_BOOL);
     try {
         $stmt->execute();
         // check number of rows affected by previous insert
         if ($stmt->rowCount() == 1) {
            $success = "$email has been registered. You may now log in.";
         }
     }catch(PDOException $e){
        if ($e->getCode() == 23000)
            $errors[] = "Email is already in use. Please use another email address.";
        else 
            $errors[] = 'Sorry, there was a problem with the database.';
    }
  }
?>

任何帮助将不胜感激!提前谢谢!

1 个答案:

答案 0 :(得分:0)

在您定义的顶级代码段中

$mailinglist = trim($_POST['mailingListCheckbox']);

然而,在您引用的第二段代码中

$stmt->bindParam(':mailingList', $mailingListValue, PDO::PARAM_BOOL);

您需要将第一部分更改为

$mailingListValue = ....

修改

上述答案是错误的,实际上可能是这样: -

if(!isset($ mailingList)){

“L”大写