con.php
<?php
$_HOST="localhost";
$_USER="root";
$_PASS="";
$_DBNM="test";
$con=mysqli_connect($_HOST, $_USER, $_PASS, $_DBNM);
if ($con === false)
{
die ("Database Connection Failed");
}
echo "Database Connection Succeed <br>";
?>
register.php
<?php
include("con.php");
$username=$_POST['username'];
$password=$_POST['password'];
$level=$_POST['level'];
$sql = "select username from user where username='$username'";
$data = mysqli_query($con,$sql);
if(mysqli_num_rows($data) > 0)
{
echo '<script language="JavaScript">alert("Username Already Exist!");
document.location:"register.html"</script>';
}
else
{
$query = $con->query("INSERT INTO user (username, password, level)
VALUES ('$username', '$password', '$level')");
echo '<script language="JavaScript">alert("Registration Succeed!");
document.location:"index.php"</script>';
}
?>
so the code is working for not inserting account if the username already exist, but it doesn't show the echo and it got stuck on the con.php. Also, When I enter the non-exist username, the account added successfully but the echo didn't apppear.