在php错误重定向登录和注册

时间:2018-05-01 10:48:29

标签: php html5 mysqli

注册表单css和html

<div class="col-md-12">
    <div class="box">
        <div class="box-header">
            <center>
                <h2>Register A New Account</h2>
            </center>
        </div>
        <!-- box-header ends -->

        <form action="signup.php" enctype="multipart/form-data" method="post">
            <div class="form-group">
                <label>FIRST NAME</label> <input class="form-control" name="first" required="" type="text">
            </div>
             <!--form group -->

            <div class="form-group">
                <label>LAST NAME</label> <input class="form-control" name="last" required="" type="text">
            </div>
            <!-- form group -->

            <div class="form-group">
                <label>Email</label> <input class="form-control" name="email" required="" type="text">
            </div>
            <!-- form group -->

            <div class="form-group">
                <label>USER NAME</label> <input class="form-control" name="uid" required="" type="text">
            </div>
            <!-- form group -->

            <div class="form-group">
                <label>PASSWORD</label> <input class="form-control" id="pwd" name="pwd" onchange="validatePassword()" required="" type="password">
            </div>
            <!-- form group -->

            <div class="form-group">
                <label>CONFIRM PASSWORD</label> <input class="form-control" id="cpwd" name="cpwd" required="" type="password">
            </div>
            <!-- form group -->

            <div class="form-group">
                <label>COUNTRY</label> <input class="form-control" name="country" required="" type="text">
            </div>
            <!-- form group -->

            <div class="form-group">
                <label>CITY</label> <input class="form-control" name="city" required="" type="text">
            </div>
            <!-- form group -->

            <div class="form-group">
                <label>ADDRESS</label> <input class="form-control" name="address" required="" type="text">
            </div>
            <!-- form group -->

            <div class="form-group">
                <label>CONTACT</label> <input class="form-control" name="phone" required="" type="text">
            </div>
            <!-- form group -->

            <div class="form-group">
                <label>PROFILE PICTURE</label> <input class="form-control" name="image" required="" type="file">
            </div>
            <!-- form group -->

            <div class="text-center">
                <button class="btn btn-primary" name="submit" type="submit"><i class="fa fa-user-md" style="padding:0px 5px;"></i>Sign Up</button>
            </div>
            <!-- text-center -->
        </form>
        <!-- form ends -->
    </div>
    <!-- box ends -->
</div>
<!-- col-m-9 ends -->

php代码进入数据库并验证

<?php
use PHPMailer\PHPMailer\PHPMailer;

if (isset($_POST['submit'])) {
    include_once 'includes/db.php';

    $first     = mysqli_real_escape_string($conn, $_POST['first']);
    $last      = mysqli_real_escape_string($conn, $_POST['last']);
    $email     = mysqli_real_escape_string($conn, $_POST['email']);
    $uid       = mysqli_real_escape_string($conn, $_POST['uid']);
    $pwd       = mysqli_real_escape_string($conn, $_POST['pwd']);
    $country   = mysqli_real_escape_string($conn, $_POST['country']);
    $city      = mysqli_real_escape_string($conn, $_POST['city']);
    $phone     = mysqli_real_escape_string($conn, $_POST['phone']);
    $address   = mysqli_real_escape_string($conn, $_POST['address']);
    $img       = mysqli_real_escape_string($conn, $_FILES['image']['name']);
    $temp_name = mysqli_real_escape_string($conn, $_FILES['image']['tmp_name']);
    $userip    = getUserIP();
    move_uploaded_file($temp_name, "customer/customer_images/$img");
    //Error handlers
    //Check for empty fields
    if (empty($first) || empty($last) || empty($email) || empty($uid) || empty($pwd) || empty($country) || empty($city) || empty($phone) || empty($address) || empty($img)) {
        echo "<script>alert('Empty Fields.Please fill all the details');
        window.location.href='../signup.php?signup=empty';
        </script>";
        exit();
    } else {
        //check if input character are valid
        if (!preg_match("/^[a-zA-z]*$/", $first) || !preg_match("/^[a-zA-z]*$/", $last)) {
            echo "<script>alert('Invalid Characters');
            window.location.href='../signup.php?signup=invalid';
            </script>";
            exit();
        } else {
            //check valid email
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                echo "<script>alert('Ivalid email id');
                window.location.href='../signup.php?signup=email';
                </script>";
                exit();
            } else {
                //check if username is same as others'
                $sql         = "SELECT * FROM users WHERE user_uid='$uid' AND user_email='$email'";
                $result      = mysqli_query($conn, $sql);
                $resultCheck = mysqli_num_rows($result);

                if ($resultCheck > 0) {
                    echo "<script>alert('email or username already exist.Please try again with new email id');
                    window.location.href='../signup.php?signup=userexist';
                    </script>";
                    exit();
                } else {
                    //Hash the password
                    $hashedpwd = password_hash($pwd, PASSWORD_BCRYPT);
                    $token     = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789!@#$%^&*()';
                    $token     = str_shuffle($token);
                    $token     = substr($token, 0, 10);


                    $sel_cart   = "select * from cart where ip_add='$user_ip'";
                    $run_cart   = mysqli_query($conn, $sel_cart);
                    $cart_check = mysqli_num_rows($run_cart);


                    //Insert user into database
                    $sql = "INSERT INTO users (user_first,user_last,user_email,user_uid,user_pwd,isEmailConfirmed,token,country,city,address,phone,image,userip) VALUES('$first','$last','$email','$uid','$hashedpwd','0','$token','$country','$city','$address','$phone','$img','$userip');";
                    mysqli_query($conn, $sql);

                    if ($cart_check > 0) {
                        $_SESSION['user_email'] = $email;
                        echo "<script>alert('Registration successfull.Please check your mail to verfiy the details.Thank you');
                             window.open('checkout.php','_self');
                        </script>";
                    } else {
                        $_SESSION['user_email'] = $email;
                        echo "<script>alert('Registration successfull.Please check your mail to verfiy the details.Thank you');
                    window.location.href='../signup.php?signup=successfull';
                        window.open('index.php','_self');
                        </script>";

                    }
                }
            }
        }

    }



} else {
    header("Location: ../signup.php");
    exit();
}

这是我的注册代码。这里的问题是,当未注册并将产品添加到购物车中的用户进入注册页面时。注册后,他需要进入结账页面。如果购物车内没有商品,则页面需要重定向到index.php。我面临的问题是当购物车有一个商品时数据被输入数据库,然后在注册后重定向到index.php而不是checkout.php。

1 个答案:

答案 0 :(得分:0)

中存在小变量名称错误
$sel_cart   = "select * from cart where ip_add='$user_ip'";

您添加了$user_ip而不是$userip。通过以下方式更新您的代码,

$sel_cart   = "select * from cart where ip_add='$userip'";