2014-02-27 55 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語句後開始,對於條件而言,它看起來是否正確? –