Hey guys, I’m new to PHP and I’m creating a login and register using PHP but then how do I actually connect them together. Cause I keep on getting connection failed on the webpages when direct it. And I’m using xampp to do this too.
The database name is database and the table name is users
Below are my code:-
Server Code
<?php
session_start();
//Create Connection
$connection = mysqli_connect('localhost', 'root', '');
mysqli_select_db($connection, 'database');
//Check Connection
if(!$connection -> connect_error){
die("Connection Failed!" . mysqli_connect_error());
}
$email = $_POST['email'];
$fullname = $_POST['fullname'];
$password = $_password['password'];
$birthday = $_POST['birthday'];
?>
Sign Up Code
<?php
include('server.php');
$sqlCheck = "select * from users where email = '$email' ";
$result = mysqli_query($connection, $sqlCheck);
$num = mysqli_num_rows($result);
//Check if the email have registered
if($num == 1){
echo "Email have been registered!";
} else {
$sqlInsert = "insert into users(email,fullname,password,birthday)
VALUES ('$email','$fullname','$password','$birthday')";
mysqli_query($connection, $sqlInsert);
echo "Successfully Registered!";
}
?>
Login Code
<?php
include('server.php');
$sqlCheck = "select * from usertable where email = '$email' && password='$password'";
$result = mysqli_query($connection, $sqlCheck);
$num = mysqli_num_rows($result);
//Check if the email have registered
if($num == 1){
header('location: user_home.php');
} else {
header('location: login.php');
}
?>
Error