-1

I want to check if the username already exists and throw an error message if exist, any tips how can I do it? I've already tried to search but only found mysql_errno but it won't work for me.

  if ($valid) {
        $pdo = Database::connect();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        //$password = md5($password);
        $sql = "INSERT INTO users (username,password,role) values(?, ?, ?)";
        $q = $pdo->prepare($sql);
        $q->execute(array($username,$password,$role));
        Database::disconnect();
        header("Location: index.php");
    }

sd

4 Answers4

1

If you want to check if the username is already used :

SELECT * FROM users WHERE username = :username

If you return one row : you throw your error, else you do your INSERT

So your code should look like this :

if ($valid) {
    $pdo = Database::connect();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql_check = "SELECT * FROM users WHERE username = :username";
    $test = $pdo->prepare($sql_check);
    $test->bindParam(':username', $username); 
    $test->execute;

Now here is two solution to check if you have one row :

    if($test->rowCount() > 0) {
        // error
    }      

or

    $user = $test->fetch();

    if (!empty($user)) {
        // error
    }

And now if you don't have error, do you insert :

    else {
        //$password = md5($password);
        $sql = "INSERT INTO users (username,password,role) values(?, ?, ?)";
        $q = $pdo->prepare($sql);
        $q->execute(array($username,$password,$role));
        Database::disconnect();
        header("Location: index.php");
    }
}
Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36
0

Two solutions:

You could make the username field unique, this way, MYSQL would throw an error if you try to insert a line with an existing username.

$sql = "INSERT INTO users (username,password,role) values(?, ?, ?)";
$q = $pdo->prepare($sql);
$result = $q->execute(array($username,$password,$role));
if(!$result) {
    // print out your error message
}

Otherwise, make a select with the wanted username and see if a row is returned (= already used) or not (=available).

$sql = "SELECT COUNT(*) as count FROM users WHERE username = ?";
$q = $pdo->prepare($sql);
$q->execute(array($username));
$result = $q->fetchAll();
// test result, if 1 > print error, etc
Jules R
  • 553
  • 2
  • 18
  • I've already that field as ´UNIQUE´ but I want to display an error message on the php page, and not the usual error message from MySQL – André Miguel Dec 20 '18 at 10:43
  • @AndréMiguel `execute` return true or false if the query was successful or not, you can make a condition on it to display your own error message – Jules R Dec 20 '18 at 10:48
  • And i've tried this `$sql2 = "SELECT COUNT(*) as count FROM users WHERE username = ?"; $q2 = $pdo->prepare($sql2); $q2->execute(array($username)); $result = $q2->fetchAll(); if ($result >1){ echo '

    ERROR

    '; } else{ $sql = "INSERT INTO users (username,password,role) values(?, ?, ?)"; $q = $pdo->prepare($sql); $q->execute(array($username,$password,$role)); }` but it skips the error message
    – André Miguel Dec 20 '18 at 11:07
  • does result contain directly the count ? (hint: print_r it to see the content) – Jules R Dec 20 '18 at 12:01
  • `Array ( [0] => Array ( [count] => 1 [0] => 1 ) )` – André Miguel Dec 20 '18 at 12:05
  • Then test $result[0]['count'] ^^' – Jules R Dec 20 '18 at 12:25
  • Don't execute the insert query if there's already an existing row dude ^^ – Jules R Dec 20 '18 at 13:13
  • I'm not executing it, it's on `else` so it shouldn't be any existing row – André Miguel Dec 20 '18 at 13:33
-2
SELECT * FROM users WHERE username = $username

If found show error, else insert

Rubin bhandari
  • 1,873
  • 15
  • 20
-2
 <?php
     $getUniqueUsername=$con->query("select * from user where name='$user_name");
     $rowCount=$getUniqueUsername->num_rows;

     if($rowCount) {          
          $insertQuery=$con->query("insert into table_name (field_name) Values('".$values."'));
     }
?>
treyBake
  • 6,440
  • 6
  • 26
  • 57
Jawa
  • 17
  • 4