I work on a code for a few hours and I can't find what I did wrong.
I want that if the username or the email exists, to be redirected to the register page. If they don't exist, just write into the database and then being redirected to another page.
But, there's an error 500 or it works like if the username and the password weren't already used, but they're actually used..
I've made many versions, but I'll show you the last one with an error 500, because I don't have the former version.
<?php
$username = $_POST['username'];
$first_name = $_POST['first'];
$last_name = $_POST['last'];
$password = $_POST['password'];
$email = $_POST['email'];
$birth = $_POST['birth'];
$date = date('Y-m-d H:i:s');
$servername = "localhost";
$sqlUser = "USER";
$sqlPassword = "PASSWORD";
try {
$conn = new PDO("mysql:host=$servername;dbname=DBNAME", $sqlUser, $sqlPassword);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
}
$sqlu = $conn->query("SELECT username from users where username = '$username'");
if($sqlu->fetch() == $username){
header('location: http://musicaltry.com/en/register.php');
}
$sqle = $conn->query("SELECT email from users where emaile = '$email'");
if($sqle->fetch() == $email){
header('location: http://musicaltry.com/en/register.php');
}
else{
$stmt = $conn->prepare("INSERT INTO users (username, first_name, last_name, password, email, birth) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssss", $username, $first_name, $last_name, $password, $email, $birth);
$stmt->execute();
$conn->close();
header('location: http://musicaltry.com/en/registered.php');
}
?>
I hope you'll be able to help me :).