My code is not sending data to the database but it lets me have my own error like DB error or something. tried check my database it seems to be perfect. in the post method the register is the name of the button
<?php
if(isset($_POST['register']))
{
@$username=$_POST['username'];
@$password=$_POST['password'];
@$cpassword=$_POST['cpassword'];
@$role=$_POST['role'];
if($password==$cpassword)
{
$query = "SELECT * FROM usersample WHERE username='$username'";
//echo $query;
$query_run = mysqli_query($con,$query);
//echo mysql_num_rows($query_run);
if($query_run)
{
if(mysqli_num_rows($query_run)>0)
{
echo '<script type="text/javascript">alert("This Username Already exists.. Please try another username!")</script>';
}
else
{
$query = "INSERT INTO usersample(username,password,role)" ." values('$username','$password','$role')";
$query_run = mysqli_query($con,$query);
if($query_run)
{
echo '<script type="text/javascript">alert("User Registered.. Welcome")</script>';
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
header( "Location: index.php");
}
else
{
echo '<p class="bg-danger msg-block">Registration Unsuccessful due to server error. Please try later</p>';
}
}
}
else
{
echo '<script type="text/javascript">alert("DB error")</script>';
}
}
else
{
echo '<script type="text/javascript">alert("Password and Confirm Password do not match")</script>';
}
}
else
{
}
?>
This is my dbconnect and i already doublechecked everything and it seems there's not mistake in my code dbconnect.php
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$database = "fransdemo";
$con = new mysqli($hostname, $username, $password) or die ("connection failed");
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>