I have the following code to validate if an entry is duplicated or not, but instead of showing the error it just skips to index.php
and I need it to show the error message.. can someone help me out please?
if ($valid) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//$password = md5($password);
$sql2 = "SELECT COUNT(*) as count FROM users WHERE username = ?";
$q2 = $pdo->prepare($sql2);
$q2->execute(array($username));
$result = $q2->fetchAll();
if ($result >1){
echo '<script language="javascript">';
echo 'alert("user already exists")';
echo '</script>';
}else{
$sql = "INSERT INTO users (username,password,role) values(?, ?, ?)";
$q = $pdo->prepare($sql);
$q->execute(array($username,$password,$role));
}
Database::disconnect();
header("Location: index.php");
}