2017-09-09 19 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

エラーがありません。それは奇妙なことだ –

関連する問題