Hello I have while adding a login user I want to check if this user is already in DB, it will display a message if not it will be added to DB: This is the form in html:
<form name="inscription" method="post" action="03insert.php">
login: <input type="text" name="login" value=""/>
<input type="submit" name="submit" value="Inscription"/>
</form>
And this the code in PHP:
<?php
$connection = ConnectionBD();
if( isset($_POST['submit']) ){
$login = $_POST['login'];
$sql_1 = "SELECT * FROM users WHERE user_login = :login";
$query = $connection->prepare($sql_1);
$query->bindParam(':login', $login, PDO::PARAM_STR);
$query->execute();
$count = $query->rowCount();
if ($count > 0){
echo 'This login already exist';
}
else
{
$sql_2 = 'INSERT INTO users VALUES(null,"'.$login.'")';
$result = $connection->exec( $sql_2 );
if( $result > 0 ){
echo 'Registered successfully';
}else{ echo 'ERROR !<br><br>'; }
}
}
unset( $connection );
?>
I don't have error but It add the user even its exist ..Please help!!