2013-07-25 74 views
1

在我最近的話題後,我發現我的註冊頁面沒有插入到mysql 首先我知道我仍在使用mysql,但是當evrything正在工作時我將其更改爲pdo。窗體函數註冊沒有插入數據庫

所以這裏的

以下部分被用來將其插入到數據庫

形式

<div id="registerform"> 
    <form action="process.php" method="POST"> 
    <ul> 
     <li>Username : 
     <br /> 
     <?php echo $form->error("user"); ?></li> 
     <li><input type="text" name="user" maxlength="30" value="<?php echo $form->value("user"); ?>"></li> 
     <li>Password : 
     <br /> 
     <?php echo $form->error("pass"); ?></li> 
     <li><input type="password" name="pass" maxlength="30" value="<?php echo $form->value("pass"); ?>"></li> 
     <li>Email : 
     <br /> 
     <?php echo $form->error("email"); ?></li> 
     <li><input type="text" name="email" maxlength="50" value="<?php echo $form->value("email"); ?>"></li> 
     <li>First Name :<br /> 
     <?php echo $form->error("clan"); ?></li> 
     <li><input type="text" name="clan" maxlength="30" value="<?php echo $form->value("clan"); ?>" /></li> 
     <li>Last Name :<br /> 
     <?php echo $form->error("namers"); ?></li> 
     <li><input type="text" name="namers" maxlength="30" value="<?php echo $form->value("namers"); ?>" /></li> 
     <li><input type="hidden" name="subjoin" value="1"><input type="submit" value="Register!"></li> 

    </ul> 
    </form> 
</div> 

下面的代碼是

過程中的事情。 php

function procRegister(){ 
    global $session, $form; 
    /* Convert username to all lowercase (by option) */ 
    if(ALL_LOWERCASE){ 
    $_POST['user'] = strtolower($_POST['user']); 
    } 
    /* Registration attempt */ 
    $retval = $session->register($_POST['user'], $_POST['pass'], $_POST['email'], $_POST['clan'], $_POST['namers']); 

    /* Registration Successful */ 
    if($retval == 0){ 
    $_SESSION['reguname'] = $_POST['user']; 
    $_SESSION['regsuccess'] = true; 
    header("Location: ".$session->referrer); 
    } 
    /* Error found with form */ 
    else if($retval == 1){ 
    $_SESSION['value_array'] = $_POST; 
    $_SESSION['error_array'] = $form->getErrorArray(); 
    header("Location: ".$session->referrer); 
    } 
    /* Registration attempt failed */ 
    else if($retval == 2){ 
    $_SESSION['reguname'] = $_POST['user']; 
    $_SESSION['regsuccess'] = false; 
    header("Location: ".$session->referrer); 
    } 
    } 

session.php文件(檢查在形狀誤差)

function register($subuser, $subpass, $subemail, $subclan, $subnamers){ 
     global $database, $form, $mailer; //The database, form and mailer object 


      /* Username error checking */ 
      $field = "user"; //Use field name for username 
      if(!$subuser || strlen($subuser = trim($subuser)) == 0){ 
      $form->setError($field, "* Username not entered"); 
     } 
     else{ 
     /* Spruce up username, check length */ 
     $subuser = stripslashes($subuser); 
     if(strlen($subuser) < 5){ 
      $form->setError($field, "* Username below 5 characters"); 
     } 
     else if(strlen($subuser) > 30){ 
      $form->setError($field, "* Username above 30 characters"); 
     } 
     /* Check if username is not alphanumeric */ 
     else if(!ctype_alnum($subuser)){ 
      $form->setError($field, "* Username not alphanumeric"); 
     } 
     /* Check if username is reserved */ 
     else if(strcasecmp($subuser, GUEST_NAME) == 0){ 
      $form->setError($field, "* Username reserved word"); 
     } 
     /* Check if username is already in use */ 
     else if($database->usernameTaken($subuser)){ 
      $form->setError($field, "* Username already in use"); 
     } 
     /* Check if username is banned */ 
     else if($database->usernameBanned($subuser)){ 
      $form->setError($field, "* Username banned"); 
     } 
     } 

     /* Password error checking */ 
     $field = "pass"; //Use field name for password 
     if(!$subpass){ 
     $form->setError($field, "* Password not entered"); 
     } 
     else{ 
     /* Spruce up password and check length*/ 
     $subpass = stripslashes($subpass); 
     if(strlen($subpass) < 4){ 
      $form->setError($field, "* Password too short"); 
     } 
     /* Check if password is not alphanumeric */ 
     else if(!preg_match("/^([0-9a-z])+$/", ($subpass = trim($subpass)))){ 
      $form->setError($field, "* Password not alphanumeric"); 
     } 
     /** 
      * Note: I trimmed the password only after I checked the length 
      * because if you fill the password field up with spaces 
      * it looks like a lot more characters than 4, so it looks 
      * kind of stupid to report "password too short". 
      */ 
     } 

     /* Email error checking */ 
     $field = "email"; //Use field name for email 
     if(!$subemail || strlen($subemail = trim($subemail)) == 0){ 
     $form->setError($field, "* Email not entered"); 
     } 
     else{ 
     /* Check if valid email address */ 
     if(!filter_var($subemail, FILTER_VALIDATE_EMAIL)){ 
      $form->setError($field, "* Email invalid"); 
     } 
     $subemail = stripslashes($subemail); 
     } 
     /* First Name error checking */ 
     $field = "clan";  //Use field name for clan name 
     if(!$subclan || strlen($subclan = trim($subclan)) == 0){ 
      $form->setError($field, "* Clan not entered"); 
     } 

     /* Last Name error checking */ 
     $field = "namers";  //Use field name for Runescape name 
     if(!$subnamers|| strlen($subnamers = trim($subnamers)) == 0){ 
      $form->setError($field, "* Runescape name not entered"); 
     } 
     /* Errors exist, have user correct them */ 
     if($form->num_errors > 0){ 
     return 1; //Errors with form 
     } 
     /* No errors, add the new account to the database*/ 
     else{ 
     if($database->addNewUser($subuser, md5($subpass), $subemail, $subclan, $subnamers)){ 
      if(EMAIL_WELCOME){ 
       $mailer->sendWelcome($subuser, $subemail, $subpass, $subclan, $subnamers); 
      } 
      return 0; //New user added succesfully 
     }else{ 
      return 2; //Registration attempt failed 
     } 
     } 


    } 

database.php中(插入的東西)

 function addNewUser($username, $password, $email, $clan, $namers){ 
     $time = time(); 
     /* If admin sign up, give admin user level */ 
     if(strcasecmp($username, ADMIN_NAME) == 0){ 
     $ulevel = ADMIN_LEVEL; 
     }else{ 
     $ulevel = USER_LEVEL; 
     } 
     $q = "INSERT INTO ".TBL_USERS." VALUES ($username, $password, '0', $ulevel, $email, $time, $clan, $namers)"; 
     return mysql_query($q, $this->connection); 
    } 

正在收到以下錯誤

We're sorry, but an error has occurred and your registration for the username (name), could not be completed. 
Please try again at a later time. 

下面的代碼是該錯誤

<?php 
/** 
* The user is already logged in, not allowed to register. 
*/ 
if($session->logged_in){ 
    echo "<h1>Registered</h1>"; 
    echo "<p>We're sorry <b>$session->username</b>, but you've already registered. " 
     ."<a href=\"main.php\">Main</a>.</p>"; 
} 
/** 
* The user has submitted the registration form and the 
* results have been processed. 
*/ 
else if(isset($_SESSION['regsuccess'])){ 
    /* Registration was successful */ 
    if($_SESSION['regsuccess']){ 
     echo "<h1>Registered!</h1>"; 
     echo "<p>Thank you <b>".$_SESSION['reguname']."</b>, your information has been added to the database, " 
      ."you may now <a href=\"main.php\">log in</a>.</p>"; 
    } 
    /* Registration failed */ 
    else{ 
     echo "<h1>Registration Failed</h1>"; 
     echo "<p>We're sorry, but an error has occurred and your registration for the username <b>".$_SESSION['reguname']."</b>, " 
      ."could not be completed.<br>Please try again at a later time.</p>"; 
      print_r($_SESSION); 


    } 
    unset($_SESSION['regsuccess']); 
    unset($_SESSION['reguname']); 
} 

的問題是,他沒有插入任何物件數據庫

它可能是很多的代碼,但我希望它足夠的信息對這個

+0

後創建此字符串'很抱歉,但是已經發生了錯誤,您的用戶名(名稱)登記,不能completed.' – user1477388

+2

「的代碼仍然使用MySQL,但我將其更改爲pdo當evrything正在工作時「 - 所以你要讓它工作,然後再破壞它,然後再使它工作。不要浪費你的時間。現在切換到PDO *,並與現代圖書館合作。 – Quentin

+0

當mysql_query返回false時,您需要回顯mysql_error(),以便您知道哪裏出了問題。回顯'$ q'也是有幫助的。 – Barmar

回答

7

INSERT查詢需要VALUES中varchar的引號。

$q = "INSERT INTO ".TBL_USERS." VALUES ('$username', '$password', '0', '$ulevel', '$email', '$time', '$clan', '$namers')"; 
+0

wouw這麼簡單thingy修好了!謝了哥們! –

+0

@JacobBrol歡迎您:) – Konsole

+0

@DaGhostmanDimitrov時間限制:)現已接受 –