2017-09-09 102 views
1

你好,stackoverflow社區。 我目前正在開發一個小型學校項目, 但是無法創建註冊。 它似乎沒有發佈任何數據到我的數據庫。 不知道是什麼原因造成的。因爲我可以手動插入來自phpmyadmin的數據! 我現在更喜歡MYSQL和PHP。 是的,我使用php做了一個mysql連接到我的數據庫。 在這方面沒有問題。下面註冊沒有插入數據

代碼:

<div class="errors-container"> 
<?php 
    if (isset($_POST['registerBtn'])) 
    { 
      $username = $_POST['username']; 
      $password = $_POST['passwd']; 
      $repeat = $_POST['rpasswd']; 
      $email = $_POST['email']; 
      $terms = $_POST['terms']; 
      $errors = array(); 
      if (empty($username) || empty($password) || empty($repeat) || empty($email)) 
      { 
       $errors[] = 'Please fill in all required fields.'; 
      } 
      $checkUsername = $odb -> prepare("SELECT * FROM `users` WHERE `username`= :username"); 
      $checkUsername -> execute(array(':username' => $username)); 
      $countUsername = $checkUsername -> rowCount(); 
      if ($countUsername != 0) 
      { 
      $errors[] = 'The username you have entered is already in use.'; 
      } 
      $checkEmail = $odb -> prepare("SELECT * FROM `users` WHERE `email`= :email"); 
      $checkEmail -> execute(array(':email' => $email)); 
      $countEmail = $checkEmail -> rowCount(); 
      if ($countEmail0 != 0) 
      { 
       $errors[] = 'The email you have entered is already in use.'; 
      } 
       if (strlen($_POST['passwd']) < 4) { 
       $errors[] = 'The username you have entered is too short.'; 
       } 
       if (strlen($_POST['username']) > 15) { 
       $errors[] = 'The username you have entered is too long.'; 
       } 
      if (!filter_var($email, FILTER_VALIDATE_EMAIL)) 
      { 
       $errors[] = 'You have entered an invalid e-mail address.'; 
      } 
      if (!ctype_alnum($username)) 
      { 
       $errors[] = 'The username you have entered is invalid.'; 
      } 
      if ($password != $repeat) 
      { 
       $errors[] = 'The passwords you have entered does not match.'; 
      } 
      if ($terms != 'agree') 
      { 
       $errors[] = 'You have to agree t.o.s before using our service.'; 
      } 
      if (empty($errors)) 
      { 
       $sha = hash("sha512", $password); 
       $activation = generateRandomString(); 
       $insertUser = $odb -> prepare("INSERT INTO `users` VALUES(NULL, :username, :password, :email, 0, 0, 0, 0, 0)"); 
       $insertUser -> execute(array(':username' => $username, ':password' => $sha, ':email' => $email)); 
       //Send mail here 

       echo '<div class="alert alert-success fade in"><button type="button" class="close close-sm" data-dismiss="alert"><i class="fa fa-times"></i></button><strong>Success!</strong> You have registered your account successfully! Redirecting..</div><meta http-equiv="refresh" content="3;url=login.php">'; 
      } 
      else 
      { 
       echo '<div class="alert alert-block alert-danger fade in"><button type="button" class="close close-sm" data-dismiss="alert"><i class="fa fa-times"></i></button><strong>Oops!</strong><br />'; 
       foreach($errors as $error) 
       { 
        echo '- '.$error.'<br />'; 
       } 
       echo '</div>'; 
      } 
     } 
?> 
</div> 

<form method="post" role="form" id="register"> 
    <div class="form-group"> 
    <label for="Username">Username</label> 
    <input type="text" class="form-control" required name="username" id="username" placeholder="Username" autocomplete="off" autofocus/> 
    </div> 
    <div class="form-group"> 
    <label for="c-email">Email</label> 
    <input type="email" class="form-control" required name="email" id="email" placeholder="E-Mail Address" autocomplete="off" /> 
    </div> 
    <div class="form-group"> 
    <label for="pwd">Password:</label> 
    <input type="password" class="form-control" required name="passwd" id="passwd" placeholder="Password" autocomplete="off" /> 
    </div> 
    <div class="form-group"> 
    <label for="c-pwd">Confirm Password:</label> 
    <input type="password" class="form-control" required id="rpasswd" name="rpasswd" placeholder="Confirm Password" autocomplete="off" /> 
    </div> 



    <div class="checkbox"> 
    <label><input type="checkbox" name="terms" value="agree"> I agree to the <a>terms &amp; conditions</a></label> 
    </div> 
    <input class="login-btn btn-block" type="submit" name="registerBtn" value="Sign Up"><i class="fa-plus"></i> 
</form> 
+0

http://prntscr.com/gj147u –

回答

1

有一個問題是:(!$ countEmail = 0),如果(!$ countEmail0 = 0),則必須將其更改爲,如果,要注意你的變量名,這是smtg其他嘗試:

$insertUser = $odb -> prepare("INSERT INTO `users` VALUES(NULL, :username, :password, :email, 0, 0, 0, 0, 0)"); 
$insertUser->bindParam(':username', $username); 
$insertUser->bindParam(':password', $password); 
$insertUser->bindParam(':email', $email); 
$insertUser->execute(); 
+0

這似乎並沒有解決它。 –

+0

好的,你能分享一下結果嗎? (例如錯誤) –

+0

沒有任何錯誤。這是奇怪的事 –