I am new to PHP so i am trying some very basic SQL queries. Basically the first SELECT query works fine and tell me if there's already a similar pseudo in the database, but then if there's isn't and I try to add it, it always returns Error and nothing is added into the database. Is there any way to have a more comprehensive error ? Thanks a lot if you can help
<?php
$pseudo = 'Adrien';
try{
$db = new PDO(
'mysql:host=localhost;dbname=tictactoe;charset=utf8',
'root',
'root');
}
catch (Exception $e)
{
die('Error : ' . $e->getMessage());
}
$sqlquery = $db->prepare('SELECT * FROM joueur WHERE pseudo= :pseudo');
$sqlquery->execute([ 'pseudo' => $pseudo ]);
$results = $sqlquery->fetchAll();
if(sizeof($results) >0 )
{
echo 'This pseudo already exists.';
}
else
{
$sqlinsert ='INSERT INTO joueur (pseudo) VALUES ( :pseudo)';
$insertPseudo= $db->prepare($sqlinsert);
if($insertPseudo->execute(['pseudo' => $pseudo]))
{
echo 'Success';
}
else
{
echo 'Error';
}
echo 'Welcome ' . $pseudo;
}
?>