How to connect Database to HTML page

Jul 31, 2020
7
0
1
pakistan
cPanel Access Level
Website Owner
Hey,
I have created login page. I have created database as well and created user and their priviliges as well. I want to connect login.html page with database. But i do not know how to do this. Please anyone help me. I want to connect with DB because if a registered user come again data can be verified from DB if not move to Register.html page.
 

andrew.n

Well-Known Member
Jun 9, 2020
138
30
28
EU
cPanel Access Level
Root Administrator
First you have to create a database, a database user and a password. Login to cPanel, Go to MySQL database, create database there then go back and create a user. After that select the database and user then add permissions and you are good to go.
 

andrew.n

Well-Known Member
Jun 9, 2020
138
30
28
EU
cPanel Access Level
Root Administrator
What is the error you are getting exactly?
 
Jul 31, 2020
7
0
1
pakistan
cPanel Access Level
Website Owner
I have two files login.php and server.php. I want as a new user come it should enter usrname and password then it should be store in database. As in future registered user come authenticate username and password from database then redirect to the 'dashboard.html'. But i do not know whats the issue with that code i am not redirecting the other page. Not having any value in database. Please guide me
Login. php
Code:
<?php 
ob_start();
include('server.php')
?>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="description" content=""><meta name="author" content="">
    <title>Login</title>
    <!-- Custom fonts for this template-->
    <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css" />
    <link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet" /><!-- Custom styles for this template-->
    <link href="css/sb-admin-2.min.css" rel="stylesheet" />
</head>
<body class="bg-gradient-primary">
<div class="container"><!-- Outer Row -->
<div class="row justify-content-center">
<div class="col-xl-10 col-lg-12 col-md-9">
<div class="card o-hidden border-0 shadow-lg my-5">
<div class="card-body p-0"><!-- Nested Row within Card Body -->
<div class="row">
<div class="col-lg-6 d-none d-lg-block bg-login-image"></div>

<div class="col-lg-6">
<div class="p-5">
<div class="text-center">
<h1 class="h4 text-gray-900 mb-4">Welcome Back!</h1>
</div>

<form accept-charset="UTF-8" action="login.php" class="user" method="post">
<div class="form-group"><input aria-describedby="emailHelp" class="form-control form-control-user" name="username" placeholder="Enter Username" type="text" / required></div>

<div class="form-group"><input class="form-control form-control-user" name="password" placeholder="Password" type="password" / required></div>

<div class="form-group">
<div class="custom-control custom-checkbox small"><input class="custom-control-input" id="customCheck" type="checkbox" /> <label class="custom-control-label" for="customCheck">Remember Me</label></div>
</div>
<button type="submit" class="btn btn-primary btn-user btn-block" name="login_user">Login</button>

<div class="text-center"><a class="small" href="forgot-password.html">Forgot Password?</a></div>

<div class="text-center"><a class="small" href="register.php">Create an Account!</a></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Bootstrap core JavaScript--><script src="vendor/jquery/jquery.min.js">
</script><script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- Core plugin JavaScript--><script src="vendor/jquery-easing/jquery.easing.min.js"></script>
<!-- Custom scripts for all pages--><script src="js/sb-admin-2.min.js"></script>
<?php

ob_end_flush();
?>
</body>
</html>

Server.php

Code:
<?php
session_start();

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

// connect to the database
$db = mysqli_connect('localhost', 'helperst_user', 'R^]J[aRz-QPN', 'helperst_new');

// REGISTER USER
if (isset($_POST['reg_user'])) {
  // receive all input values from the form
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $firstname = mysqli_real_escape_string($db, $_POST['firstname']);
  $lastname = mysqli_real_escape_string($db, $_POST['lastname']);
  $email = mysqli_real_escape_string($db, $_POST['email']);
  $bankname = mysqli_real_escape_string($db, $_POST['bankname']);
  $accountnumber = mysqli_real_escape_string($db, $_POST['accountnumber']);
  $phonenumber = mysqli_real_escape_string($db, $_POST['phonenumber']);
  $sponsor = mysqli_real_escape_string($db, $_POST['sponsor']);
  $password = mysqli_real_escape_string($db, $_POST['password']);
 
 
  // Finally, register user if there are no errors in the form
  if (count($errors) == 0) {
      $password = md5($password);//encrypt the password before saving in the database

      $query = "INSERT INTO users (username, firstname, lastname, email, bankname, accountnumber, phonenumber, sponsor, password)
                VALUES('$username',  '$firstname', '$lastname',  '$email', '$bankname',  '$accountnumber', '$phonenumber', '$sponsor', '$password')";
      mysqli_query($db, $query);
      $_SESSION['username'] = $username;
      $_SESSION['success'] = "You are now logged in";
    header('Location: https://helperstree.com.ng/dashboard.html');
  //echo "<script>window.top.location='http://www.google.com'</script>";
  }
}

// ...



// LOGIN 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) == 1) {
        $_SESSION['username'] = $username;
        $_SESSION['success'] = "You are now logged in";
        header('Location: https://helperstree.com.ng/dashboard.html');
      // echo "<script>window.top.location='http://www.google.com'</script>";
      }else {
          array_push($errors, "Wrong username/password combination");
      }
  }
}

?>
 

andrew.n

Well-Known Member
Jun 9, 2020
138
30
28
EU
cPanel Access Level
Root Administrator
You need to hire a PHP developer to look into your code then. This is not really a cPanel issue :(
 

cPanelLauren

Technical Support Community Manager
Staff member
Nov 14, 2017
12,377
1,129
313
Houston
You'd get the most help from looking up a tutorial on how to create this such as the following: