Check user used MySQL/PHP

Résolu
Tommy -  
 Tommy -
Bonjour,
Je me permet de soliciter votre aide pour un soucis que je n'arrive pas à régler seul, cela fait deux jours que je suis dessus et je n'arrive pas comprendre mon erreur.

Voici le soucis, je créer un site en html/php/mysql, j'ai créer une page d'inscription en php relier au mysql, j'aimerais dans cette dernière que si quelqu'un tente de creer un compte avec un nom d'utilisateurs déjà utiliser que ce dernier se vois refuser. Voici mon code, merci de votre aide.

if(isset($_POST['inscription'])) {

$Name = htmlentities($_POST['Name']);
$Password = md5($_POST['Password']);
$avatar = htmlentities($_POST['avatar']);
$Registered = strtotime(date('Y-m-d H:i:s'));
$Sex = htmlentities($_POST['Sex']);
$Steam = htmlentities($_POST['Steam']);
$Twitch = htmlentities($_POST['Twitch']);
$Age = htmlentities($_POST['Age']);
$Email = htmlentities($_POST['Email']);

$checkname = $dbc->query("SELECT * FROM {$cfg_prefix}players_stats WHERE Name = '".$Name."'");

if(mysql_num_rows($checkname) == 1)
{
echo "<h1>Error</h1>";
echo "<p>Sorry, that username is taken. Please go back and try again.</p>";
}
else
{
if(!empty($Name) || !empty($Password) || !empty($Email) || !empty($Age) || !empty($Sex)) {
$rq_add = $dbc->prepare("INSERT INTO srp_players_stats VALUES('', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$rq_add->execute(array($Name, $Password, 0, 0, 0, 0, $Registered, $Sex, $Steam, $Twitch, $Age, $Email));
echo '<div class="green_box"><i class="fa fa-check" style="margin-right:6px;"></i> Votre compte a été crée avec succès, vous pouvez maintenant vous connecter. Bienvenue sur Everyseries !</div>';
} else {
echo '<div class="red_box"><i class="fa fa-warning" style="margin-right:6px;"></i> Veuillez remplir tous les champs du formulaire.</div>';
}
}
}
?>
A voir également:

2 réponses

jordane45 Messages postés 38486 Date d'inscription   Statut Modérateur Dernière intervention   4 752
 
Bonjour,
Tu utilises quoi pour te connecter à ta BDD ?

Car je vois :
checkname = $dbc->query("SELECT * FROM {$cfg_prefix}players_stats WHERE Name = '".$Name."'");

Et juste après ça :
if(mysql_num_rows

ça n'est pas cohérent...

De plus .. ta question concernant le PHP je la déplace dans le bon forum.
merci d'y faire attention à l'avenir !

0
Tommy
 
Merci Jordane de ta réponse, effectivement c'est incohérent, j'essaye de régler ça et je te redit.
0
Tommy
 
J'ai fait ça, mais rien à faire, la page reste blanche :
<?php
if(isset($_POST['inscription'])) {

$Name = htmlentities($_POST['Name']);
$Password = md5($_POST['Password']);
$avatar = htmlentities($_POST['avatar']);
$Registered = strtotime(date('Y-m-d H:i:s'));
$Sex = htmlentities($_POST['Sex']);
$Steam = htmlentities($_POST['Steam']);
$Twitch = htmlentities($_POST['Twitch']);
$Age = htmlentities($_POST['Age']);
$Email = htmlentities($_POST['Email']);

$checkname = $dbc->query("SELECT * FROM {$cfg_prefix}players_stats WHERE Name = '".$Name."'");

if($dbc->num_rows($checkname) == 1)
{
echo "<h1>Error</h1>";
echo "<p>Sorry, that username is taken. Please go back and try again.</p>";
}
else
{
if(!empty($Name) || !empty($Password) || !empty($Email) || !empty($Age) || !empty($Sex)) {
$rq_add = $dbc->prepare("INSERT INTO srp_players_stats VALUES('', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$rq_add->execute(array($Name, $Password, 0, 0, 0, 0, $Registered, $Sex, $Steam, $Twitch, $Age, $Email));
echo '<div class="green_box"><i class="fa fa-check" style="margin-right:6px;"></i> Votre compte a été crée avec succès, vous pouvez maintenant vous connecter. Bienvenue sur Everyseries !</div>';
} else {
echo '<div class="red_box"><i class="fa fa-warning" style="margin-right:6px;"></i> Veuillez remplir tous les champs du formulaire.</div>';
}
}
}
?>
0
jordane45 Messages postés 38486 Date d'inscription   Statut Modérateur Dernière intervention   4 752
 
0
Tommy
 
Voilà ce qu'il me marque


Notice: Undefined index: avatar in /var/www/html/devsite22/login.php on line 68

Fatal error: Uncaught Error: Call to undefined method PDO::num_rows() in /var/www/html/devsite22/login.php:78 Stack trace: #0 {main} thrown in /var/www/html/devsite22/login.php on line 78


Voici la ligne 78 du code que j'ai envoyer en haut
if($dbc->num_rows($checkname) == 1)


Et la ligne 68
$avatar   	=	htmlentities($_POST['avatar']);
0
Tommy
 
Haaa j'ai réussi, merci à toi Jordan. Effectivement j'ai été voir sur internet num_rows ne marche pas en méthode PDO. Voici la correction.

<?php
if(isset($_POST['inscription'])) {

$Name = htmlentities($_POST['Name']);
$Password = md5($_POST['Password']);
$avatar = htmlentities($_POST['avatar']);
$Registered = strtotime(date('Y-m-d H:i:s'));
$Sex = htmlentities($_POST['Sex']);
$Steam = htmlentities($_POST['Steam']);
$Twitch = htmlentities($_POST['Twitch']);
$Age = htmlentities($_POST['Age']);
$Email = htmlentities($_POST['Email']);

$checkname = $dbc->query("SELECT * FROM {$cfg_prefix}players_stats WHERE Name = '".$Name."'");
$checkuser = $checkname->rowCount();

if($checkuser == 1)
{
echo "<h1>Error</h1>";
echo "<p>Sorry, that username is taken. Please go back and try again.</p>";
}
else
{
if(!empty($Name) || !empty($Password) || !empty($Email) || !empty($Age) || !empty($Sex)) {
$rq_add = $dbc->prepare("INSERT INTO srp_players_stats VALUES('', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$rq_add->execute(array($Name, $Password, 0, 0, 0, 0, $Registered, $Sex, $Steam, $Twitch, $Age, $Email));
echo '<div class="green_box"><i class="fa fa-check" style="margin-right:6px;"></i> Votre compte a été crée avec succès, vous pouvez maintenant vous connecter. Bienvenue sur Everyseries !</div>';
} else {
echo '<div class="red_box"><i class="fa fa-warning" style="margin-right:6px;"></i> Veuillez remplir tous les champs du formulaire.</div>';
}
}
}
?>
0