duanjurong1347 2017-09-09 12:31
浏览 54

注册不插入数据

Hello stackoverflow community. I'm currently developing a small school project, But having trouble creating the registration. It seems like it's not posting any data to my database. Not sure what's causing this. Since i can manually insert data from phpmyadmin! I'm rather now to MYSQL And PHP. Yes, I've made a mysql connection to my database using php. No problems on that end.

Code below:

<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>
  • 写回答

1条回答 默认 最新

  • douxi3404 2017-09-09 12:41
    关注

    There is a problèm at : if ($countEmail0 != 0) , you must change it to if ($countEmail != 0) , pay attention to your variables names, this is smtg else to try :

    $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();
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度