Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if username exists in mysql table via php? [duplicate]

Tags:

php

mysql

I am trying to figure out how to have my register php code check whether or not the registee's username is already taken, and if it is, don't register it, tell the user that it's taken. Here's my entire register processing file.

<?php
$con=mysqli_connect("localhost","root","","users");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$hpassword = hash( 'sha512', $_POST['password'] );
$eusername = mysqli_real_escape_string( $con, $_POST['username'] );
$eemail = mysqli_real_escape_string( $con, $_POST['email'] );
$fusername = str_replace(' ', '', $eusername);

$sql="INSERT INTO users (username, password, email)
VALUES
('$fusername','$hpassword','$eemail')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
mysqli_close($con);
?> 
like image 453
Trevor Zucker Avatar asked Jul 23 '13 02:07

Trevor Zucker


2 Answers

$sql=mysql_query("SELECT FROM users (username, password, email) WHERE username=$fusername");
 if(mysql_num_rows($sql)>=1)
   {
    echo"name already exists";
   }
 else
    {
   //insert query goes here
    }

you can check from database whether user exists and then paste the code

like image 174
Php developer Avatar answered Sep 30 '22 08:09

Php developer


include ('database_connection.php');
if (isset($_POST['formsubmitted'])) {
    $error = array(); 
    if (empty($_POST['username'])) { 
        $error[] = 'Please Enter a name '; 
    } else {
        $username = $_POST['username']; 
    }

    if (empty($_POST['e-mail'])) {
        $error[] = 'Please Enter your Email ';
    } else {

        if (filter_var($_POST['e-mail'], FILTER_VALIDATE_EMAIL)) {
            //for email validation (refer: http://us.php.net/manual/en/function.filter-var.php)

            $email = $_POST['e-mail'];
        } else {
            $error[] = 'Your EMail Address is invalid  ';
        }

    }

    if (empty($_POST['password'])) {
        $error[] = 'Please Enter Your Password ';
    } else {
        $password = $_POST['password'];
    }

    if (empty($error))

    { // If everything's OK...


        $query = "SELECT * FROM members  WHERE username ='$username'";
        $result = mysqli_query($dbc, $query); // here $dbc is your mysqli $link
        if (!$result) {
            echo ' Database Error Occured ';
        }

        if (mysqli_num_rows($result) == 0) { // IF no previous user is using this username.

            $query = "INSERT INTO `members` ( `username`, `email`, `password`) VALUES ( '$name', '$email', '$password')";

            $result = mysqli_query($dbc, $query);
            if (!$result) {
                echo 'Query Failed ';
            }

            if (mysqli_affected_rows($dbc) == 1) { //If the Insert Query was successfull.

                // Send an email

                // Finish the page:
                echo '<div class="success">Thank you for registering! A confirmation email has been sent to ' . $email . ' Please click on the Activation Link to Activate your account </div>';

            } else { // If it did not run OK.
                echo '<div class="errormsgbox">You could not be registered due to a system error. We apologize for any inconvenience.</div>';
            }

        } else { // The username is not available.
            echo '<div class="errormsgbox" >That username has already been registered.
</div>';
        }

    } else { //If the "error" array contains error msg , display them.... e.g....

        echo '<div class="errormsgbox"> <ul>';
        foreach ($error as $key => $values) {

            echo '  <li>' . $values . '</li>';

        }
        echo '</ul></div>';

    }

    mysqli_close($dbc); //Close the DB Connection

} // End of the main Submit conditional.
like image 40
Razeel Akbar Avatar answered Sep 30 '22 08:09

Razeel Akbar