2014-02-27 51 views
0

尝试将复选框的输入作为布尔值处理,以便我可以将值输入到数据库中。值是“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.'; 
    } 
    } 
?> 

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

回答

0

在你的代码上段要定义

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

然而,在你的代码的第二部分你引用

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

您需要在第一部分变为

$mailingListValue = .... 

编辑

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

如果(isset($ MAILINGLIST)!){

“L” 是大写

+0

感谢答复,错过了大写字母L,但不幸的是它仍然出现了错误。错误在使用if语句后开始,对于条件而言,它看起来是否正确? –