简体   繁体   中英

registration form php only refresh phpMyAdmin

I created a registration.php a login user.php an error.php and a server.php which errors validate and server connects my php form to database. login.php is working as is saying wrong id and/or password while registration form not working. When I click submit its just like refreshing and nothing its saved to database. Trying 3 days and can't figure why. maybe its my pc problem? I'm working with XAMPP and phpmyadmin and dreamweaver.

here is my register.php.

<?php include('server.php') ?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Registration Form</title>
<link rel="stylesheet" type="text/css" href="forms.css">
</head>
<body>
<div class="header">
    <h2>Register</h2>
</div>

<form method="post" action="register.php">
    <?php include('errors.php'); ?>
    <div class="input-group">
        <label>Name:</label>
        <input type="text" name="name">
    </div>
    <div class="input-group">
        <label>Surname:</label>
        <input type="text" name="surname">
    </div>
    <div class="input-group">
        <label>Password:</label>
        <input type="password" name="password_1">
    </div>
    <div class="input-group">
        <label>Confirm Paswword:</label>
        <input type="password" name="password_2">
    </div>
    <div class="input-group">
        <label>Student ID:</label>
        <input type="text" name="studentid">
    </div>
    <div class="input-group">
        <label>Email:</label>
        <input type="text" name="email">
    </div>
    <div class="input-group">
        <label>Course:</label>
        <input type="text" name="course">
    </div>  
    <div class="input-group">
        <center><button type="submit" name="register" class="btn">Register</button></center>
    </div>
    <p>
        Already a registered student? <a href="login.php">Sign in</a>
    </p>
</form>
</body>
</html>

and this is my server.php

<?php

    session_start();

    // variable declaration
    $name = "";
    $surname = "";
    $email = "";
    $studentid = "";
    $password_1 ="";
    $password_2 = "";
    $course = "";
    $errors = array();
    $_SESSION['success'] = "";

    // connect to database
    $db = mysqli_connect('localhost', 'root', '', 'registration');

    // if register button clicked receive all inputs from the form
    if (isset($_POST['reg_user'])) {
    $name = mysqli_real_escape_string($db, $_POST['name']);
    $surname = mysqli_real_escape_string($db, $_POST['surname']);
    $email = mysqli_real_escape_string($db, $_POST['email']);
    $studentid = mysqli_real_escape_string($db, $_POST['studentid']);
    $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
    $password_1 = md5($password_1);
    $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
    $password_2 = md5($password_2);
    $course = mysqli_real_escape_string($db, $_POST['course']);


    // ensure that form fields are filled properly
    if (empty($name)) { 
        array_push($errors, "Name is required"); 
                      }
    if (empty($surname)) { 
        array_push($errors, "Surame is required"); 
    }
    if (empty($email)) { 
        array_push($errors, "Email is required"); 
    }
    if (empty($studentid)) { 
        array_push($errors, "Student ID is required"); 
    }
    if (empty($password_1)) { 
        array_push($errors, "Password is required"); 
    }
    if (empty($course)) { 
        array_push($errors, "Course is required"); 
    }

    if ($password_1 != $password_2) {
    array_push($errors, "The two passwords do not match");
    }   

    if (count($errors) == 0) {
    $password = md5($password_1); //encrypt the password before saving in the database
    $query = "INSERT INTO users (id, name, surname, email, studentid, password, course) 
              VALUES(0,'$name','$surname', '$email', '$studentid' '$password', '$course')";
    mysqli_query($db, $query);
    $_SESSION['studentid'] = $studentid;
    $_SESSION['success'] = "You are now logged in";
    header('location: index.php');
  }

}

// login user
if (isset($_POST['login_user'])) {
  $studentid = mysqli_real_escape_string($db, $_POST['studentid']);
  $password = mysqli_real_escape_string($db, $_POST['password']);

  if (empty($studentid)) {
    array_push($errors, "Student ID is required");
  }
  if (empty($password)) {
    array_push($errors, "Password is required");
  }

  if (count($errors) == 0) {
    $password = md5($password);
    $query = "SELECT * FROM users WHERE studentid='$studentid' AND password='$password'";
    $results = mysqli_query($db, $query);
    if (mysqli_num_rows($results) == 1) {
      $_SESSION['studentid'] = $studentid;
      $_SESSION['success'] = "You are now logged in";
      header('location: index.php');
    }else {
        array_push($errors, "Wrong Student ID or Password. Please try again.");
    }
  }
}

?>

database

Problem is in your Query You take surname instead of username . Change Query From this

$query = "INSERT INTO users (name, surname, email, studentid, password, course) 
              VALUES(0,'$name','$surname', '$email', '$studentid' '$password', '$course')";

To this

$query = "INSERT INTO users (id, name, surname, email, studentid, password, course) 
              VALUES('$name','$username', '$email', '$studentid' '$password', '$course')";

And there are lots of spelling mistake in code. kindly fill relax and check all spelling.

you button name is different from the one in php

//previous code button clicked for register
if (isset($_POST['reg_user'])) {}

instead of

//button clicked for register
if (isset($_POST['register'])) {}

and also your query if your "id" an auto increment, leave it blank rather put 0, you can also do as follow

$query = "INSERT INTO users (name, surname, email, studentid, password, 
course)VALUES('$name','$surname', '$email', '$studentid' '$password', '$course')"; 

UPDATED TO HELP ANYONE WITH SIMILAR ISSUE

For anyone that may have similar code challenge, please check if every semi column, comma and dot are where they should be.

You can debug also by echoing out the values to know where the problem is probably coming from.

Also read through the comments you might pick up what you need from there. Hope this helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM