简体   繁体   English

如何让我的注册表单页面与 php 一起使用?

[英]How can i get my registration form page to work with php?

please i'm kinda new to website development.请我对网站开发有点陌生。 i tried to create a registration page to work with my database but my registration form page is not responding to the php coding page.我尝试创建一个注册页面来使用我的数据库,但我的注册表单页面没有响应 php 编码页面。 please i need your assistance.我需要你的帮助。 thank感谢

This is my registration.html page i don't know if the error is from my registration form page这是我的注册。html 页面我不知道错误是否来自我的注册表单页面

<form action="student.php" name="register" id="register" method="POST" data-aos="fade">
              <div class="form-group row">
                <div class="col-md-6 mb-3 mb-lg-0">
                  <input type="text" name="firstname" id="firstname" class="form-control" placeholder="First name" required>
                </div>
                <div class="col-md-6">
                  <input type="text" name="lastname" id="lastname" class="form-control" placeholder="Last name" required>
                </div>
              </div>

              <div class="form-group row">
                <div class="col-md-12">
                  <input type="text" name="studentid" id="studentid" class="form-control" placeholder="Student ID" value="" required>
                </div>
              </div>
             <div class="form-group row">
                <div class="col-md-12">
                  <input type="text" name="level" class="form-control" placeholder="Level" required>
                </div>
              </div>
             <div class="form-group row">
                <div class="col-md-12">
                <p class="mb-0">Gender</p>
                     <input name="gender" type="radio"  value="m" required> Male
                     <input name="gender" type="radio"  value="f" required> Female
                    
                </div>
              </div>
             <div class="form-group row">
                <div id="date-picker" class="col-md-12 md-form md-outline input-with-post-icon datepicker" inline="true">
                  <input type="text" name="dob" class="form-control" id="date9" placeholder="DD/MM/YYYY" required>
                  <i class="fas fa-calendar input-prefix"></i>
                </div>
              </div>
                <script>
                    $('.datepicker').datepicker({
                        inline: true;
                    });
                    </script>
                
             <div class="form-group row">
                <div class="col-md-12">
                  <input type="email" name="email" class="form-control" placeholder="Email" value="" required>
                </div>
              </div>
             <div class="form-group row">
                <div class="col-md-12">
                  <input type="phone" name="phonenumber" id="phonenumber" class="form-control" placeholder="+234 8179 5523 71" required>
                </div>
              </div>
             <div class="form-group row">
                <div class="col-md-12">
                  <input type="department" name="department" class="form-control" placeholder="Department" required>
                </div>
              </div>
             <div class="form-group row">
                <div class="col-md-12">
                  <input type="password" name="password" value=""  id="password" class="form-control" placeholder="Password" required>
                </div>
              </div>
             <div class="form-group row">
                <div class="col-md-12">
                  <input type="password" name="confirmpassword" id="confirmpassword" value="" class="form-control" placeholder="Confirm Password" required>
                </div>
              </div>
                
              <div class="form-group row">
                <div class="col-md-6">
                  
                  <input type="submit" name="submit" class="btn btn-primary py-3 px-5 btn-block btn-pill" value="SUBMIT">
                    
                </div>
              </div>

            </form>

This is my Php page for the form or maybe the error is from my php code.这是我的表格 Php 页面,或者错误可能来自我的 php 代码。 please help detect the problem guys.请帮助发现问题的家伙。 Thanks.谢谢。

<?php
session_start();

// initializing variables
$studentid = "";
$email    = "";
$errors = array(); 

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

// REGISTER USER
if (isset($_POST['submit'])) {
  // receive all input values from the form
  $firstname = mysqli_real_escape_string($db, $_POST['firstname']);
  $lastname = mysqli_real_escape_string($db, $_POST['lastname']);
  $studentid = mysqli_real_escape_string($db, $_POST['studentid']);
  $level = mysqli_real_escape_string($db, $_POST['level']);
  $gender = mysqli_real_escape_string($db, $_POST['gender']);
  $dob = mysqli_real_escape_string($db, $_POST['dob']);
  $email = mysqli_real_escape_string($db, $_POST['email']);
  $phonenumber = mysqli_real_escape_string($db, $_POST['phonenumber']);
  $department = mysqli_real_escape_string($db, $_POST['department']);
  $password_1 = mysqli_real_escape_string($db, $_POST['password']);
  $password_2 = mysqli_real_escape_string($db, $_POST['confirmpassword']);

  // form validation: ensure that the form is correctly filled ...
  // by adding (array_push()) corresponding error unto $errors array
  if (empty($studentid)) { array_push($errors, "Student ID is required"); }
  if (empty($email)) { array_push($errors, "Email is required"); }
  if (empty($password_1)) { array_push($errors, "Password is required"); }
  if ($password_1 != $password_2) {
    array_push($errors, "The two passwords do not match");
  }

  // first check the database to make sure 
  // a user does not already exist with the same username and/or email
  $user_check_query = "SELECT * FROM student_registra WHERE studentid='$studentid' OR email='$email' LIMIT 1";
  $result = mysqli_query($db, $user_check_query) or die(mysqli_error($db));
  $user = mysqli_fetch_assoc($result);
  
  if ($user) { // if user exists
    if ($user['studentid'] === $studentid & $user['email'] === $email) {
      array_push($errors, "Student Id already taken");
    }

    if ($user['email'] === $email) {
      array_push($errors, "email already exists");
    }
  }

  // Finally, register user if there are no errors in the form
  if (count($errors) == 0) {
    $password = md5($password_1);//encrypt the password before saving in the database

    $query = "INSERT INTO student_registra (firstname, lastname, studentid, level, gender, dob, email, phonenumber, department, password) 
              VALUES('$firstname', '$lastname', '$studentid', '$level', '$gender', '$dob', '$email', '$phonenumber', '$department', '$password')";
    mysqli_query($db, $query);
    $_SESSION['studentid'] = $studentid;
    $_SESSION['success'] = "Registration Sucessful";
    header('location: index.html');
  }
}

i don't know what seems to be the problem, because i run it the first time it worked, but when i shutdown my laptop and turn it on back again after my lunch, it stop working.我不知道问题出在哪里,因为我第一次运行它时运行它,但是当我关闭笔记本电脑并在午餐后再次打开它时,它停止工作。 Instead of it to read the.php code it's rather displaying the whole.php code and i've checked the code, i can't find what seems to be the problem.与其读取 .php 代码,不如显示整个 .php 代码,我检查了代码,但找不到问题所在。 please guys, i'll need your help in fixing this or detecting the problem.伙计们,我需要你的帮助来解决这个问题或发现问题。 thanks谢谢

EDIT, You have a lot of useless codes that make your code so slow, Like $_SESSION['success'];编辑,你有很多无用的代码让你的代码很慢,比如 $_SESSION['success']; This is not neccessary, Change your index.html to index.php and delete it because it do nothing, You can check session by student id You have two gender Inputs, how comes you assign one of them?这不是必需的,将您的 index.html 更改为 index.php 并删除它,因为它什么都不做,您可以检查 session 学生分配的两个性别,您如何? This is first mistake Secondly, Use Prepared Statements to avoid SQLI Attacks Thirdly How comes you header a html page when you're in php page?这是第一个错误其次,使用准备好的语句来避免 SQLI 攻击第三个当你在 php 页面时,你怎么 header 一个 html 页面? change index.html to index.php将 index.html 更改为 index.php

And Use this code instead:并改用此代码:

 if (count($errors) == 0) {
    $password = md5($password_1);//encrypt the password before saving in the database
    $prepared = "INSERT INTO student_registra (firstname, lastname, studentid, level, gender, dob, email, phonenumber, department, password) 
              VALUES('$firstname', '$lastname', '$studentid', '$level', '$gender', '$dob', '$email', '$phonenumber', '$department', '$password')";

    $query = $prepared;
    mysqli_query($db, $query);
    $_SESSION['studentid'] = $studentid;
    # Change your files index.html To index.php
    header('location: index.php');
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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