This question already has an answer here:
- Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors? 3 answers
- Reference - What does this error mean in PHP? 34 answers
- How to get MySQLi error information in different environments? / mysqli_fetch_assoc() expects parameter 1 to be mysqli_result 1 answer
I just wanted to create a script that allows me to check if the user trying to register isn't already in my database.
My code is split in two function register and login. One is called when we press the login button the other when we press the register button.
Now the thing is , whenever i click on register with a user that is already in my database, it will detect that he is existing and immediatly connect him.
What I want , is to reload the login page adding the message User is already existing in the database.
Could you help me please ?
This is the function register :
function register()
{
include("database.php");
$uname = $_POST['uname'];
$psw = $_POST['psw'];
$check = " SELECT User_Username FROM user WHERE User_Username = '".$uname."' ";
$result_of_match = mysqli_query($conn,$check);
$row_of_match = mysqli_fetch_array($result_of_match,MYSQLI_ASSOC);
if(mysqli_num_rows($result_of_match) >=1){
header("Location: Login.php?msg3=" . urlencode(base64_encode("User already created.")));
} else {
$sql1 = "INSERT INTO user(User_ID, User_Username, User_Password) VALUES (Null, '$uname', '$psw')";
if(mysqli_query($conn,$sql1))
{header("Location: Login.php?msg4=" . urlencode(base64_encode("User successfully created.")));}
}
}
and this is how i call both of my function
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['register'])){
register();
}
if($_SERVER ["REQUEST_METHOD"]== "POST"){
login();
}
</div>