PHP

时间:2019-06-15 22:05:07

标签: php mysql

我正在尝试创建一个注册表单,将一个新用户注册到MySQL数据库。

该代码应该注册以下数据:

  • 用户名
  • 电子邮件
  • 密码
  • 检查密码
  • 名字和姓氏
  • 出生日期
  • 电话号码
  • 街道地址

但是,当导航到页面顶部的register.php文件时,将显示以下错误:

Notice: Undefined index: username in /opt/lampp/htdocs/bank/server.php on line 21

Notice: Undefined index: email in /opt/lampp/htdocs/bank/server.php on line 22

Notice: Undefined index: password_1 in /opt/lampp/htdocs/bank/server.php on line 23

Notice: Undefined index: password_2 in /opt/lampp/htdocs/bank/server.php on line 24

Notice: Undefined index: firstandlast in /opt/lampp/htdocs/bank/server.php on line 25

Notice: Undefined index: bday in /opt/lampp/htdocs/bank/server.php on line 26

Notice: Undefined index: phone in /opt/lampp/htdocs/bank/server.php on line 27

Notice: Undefined index: street in /opt/lampp/htdocs/bank/server.php on line 28

这是server.php代码:

<?php

session_start();

//Initialising Variables

$username = "";
$email = "";

//Error Arrays
$errors = array();



//Connect to Data-base

$db = mysqli_connect('localhost', 'root', '', 'bank') or die ("could not connect to database");

//Register Users

$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
$firstandlast = mysqli_real_escape_string($db, $_POST['firstandlast']);
$bday = mysqli_real_escape_string($db, $_POST['bday']);
$phone = mysqli_real_escape_string($db, $_POST['phone']);
$street = mysqli_real_escape_string($db, $_POST['street']);

//Form Validation: Ensure that the form is correctly filled

if(empty($username)) { array_push($errors, "Username is required"); }
if(empty($email)) { array_push($errors, "Email is required"); }
if(empty($password_1)) { array_push($errors, "Password is required"); }
if(empty($firstandlast)) { array_push($errors, "First & Last name is required"); }
if(empty($bday)) { array_push($errors, "Birth Date is required"); }
if(empty($phone)) { array_push($errors, "Phone Number is required"); }
if(empty($street)) { array_push($errors, "Street Address is required"); }
if($password_1 != $password_2){ array_push($errors, "Password do not match"); }

//Check Data-base For Exsiting User With The Same Username

$users_check_query = "SELECT * FROM users WHERE username = '$username' or email = '$email' LIMIT 1";

$results = mysqli_query($db, $users_check_query);
$users = mysqli_fetch_assoc($results);

if($users) {

    if($users['username'] === $username){array_push($errors, "Username already exists");}
    if($users['email'] === $email){array_push($errors, "This Email ID already has a registered username");}

}

//Register The User IF No Errors

if(count($errors) == 0 ){

    $password = md5($password_1); //This will encrypt the password
    $query = "INSERT INTO users (username, email, password, firstandlast, bday, phone, street)
    VALUES ('$username', '$email', '$password', '$firstandlast', '$bday', '$phone', '$street')";

    mysqli_query($db, $query);
    $_SESSION['username'] = $username;
    $_SESSION['success'] = "You are now logged in";

    header('location: index.php');

}

//Log-in User

if(isset($_POST['login_user'])) {

    $username = mysqli_real_escape_string($db, $_POST['username']);
    $password = mysqli_real_escape_string($db, $_POST['password']);

    if(empty($username)){

    array_push($errors, "Username is required");
    }

    if(empty($password)){

    array_push($errors, "Password is required");
    }

    if(count($errors) == 0) {
        $password = md5($password);
        $query = "SELECT * FROM users WHERE username='$username' AND password='$password' ";
        $results = mysqli_query($db, $query);

    if(mysqli_num_rows($results)) {

        $_SESSION['username'] = $username;
        $_SESSION['success'] = "Logged in successfully";
        header('location: index.php');
    }else{
        array_push($errors, "Wrong username or password");
    }
    }
    }


?>

这是register.php代码:

<?php include('server.php') ?>

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Rose Bank - Register</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
  <style>
  .fakeimg {
    height: 200px;
    background: #aaa;
  }
  </style>
</head>
<body>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
  <a class="navbar-brand" href="BankHome.html">Home</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="collapsibleNavbar">
    <ul class="navbar-nav">
      <li class="nav-item">
        <a class="nav-link" href="#">About Us</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Locations</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Support</a>
      </li>    
    </ul>
  </div>  
</nav>

<div class="container">
    <div class="row">
        <div class="col-md-4"></div>
        <div class="col-md-4">
            <hr/>

            <h2>Please Register</h2>
            <hr/>

            <form action="register.php" method="post">

                <?php include('errors.php') ?>

                <div class="form-group">
                    <label for="username">Username:</label>
                    <input type="text" class="form-control" id="username" placeholder="Enter username" name="username" required>
                </div>

                <div class="form-group">
                    <label for="email">Email Address:</label>
                    <input type="email" class="form-control" id="email" placeholder="Enter email" name="email" required>
                </div>

                <div class="form-group">
                    <label for="password">Password:</label>
                    <input type="password" class="form-control" id="password_1" placeholder="Enter password" name="password_1" required>
                </div>

                <div class="form-group">
                    <label for="password">Confirm Password:</label>
                    <input type="password" class="form-control" id="password_2" placeholder="Confirm password" name="password_2" required>
                </div>

                <div class="form-group">
                    <label for="pwd">First Name & Last Name:</label>
                    <input type="text" class="form-control" id="firstandlast" placeholder="Enter First Name & Last Name"
                           name="firstandlast" required>
                </div>
                <div class="form-group">
                    <label for="pwd">Birth Date:</label>
                    <input type="text" class="form-control" id="bday" placeholder="Enter Birth Date"
                           name="bday" required>
                </div>
                <div class="form-group">
                    <label for="pwd">Phone Number:</label>
                    <input type="text" class="form-control" id="phone" placeholder="Enter Phone Number"
                           name="phone" required>
                </div>
                <div class="form-group">
                    <label for="email">Street Address:</label>
                    <input type="text" class="form-control" id="street" placeholder="Enter Street Address" name="street" required>
                </div>
                <div class="checkbox">
                    <label><input type="checkbox" name="remember"> Remember me</label>
                </div>
                <button type="submit" class="btn btn-primary btn-block" name="reg_user">Register</button>
            </form>
            <hr/>
              <p>Already a user? <br/><a href="log-in.php"><b>Log-in</b></a></p>
        </div>
        <div class="col-md-4"></div>
    </div>
</div>

</body>
</html>

任何帮助将不胜感激。

谢谢!

0 个答案:

没有答案