Jump to content

PHP comparing inputting data against a database.


Tweezy
Go to solution Solved by Tweezy,

Recommended Posts

Hi. I'm fairly new to PHP, but I have a lot of experience within c++ as my first language I learnt. I'm creating a simple registration / login / userCP area as a little personal project. I've hit a bit of a stump as my knowledge of php doens't quite cut it. 

 

I'm trying to compare inputted data against data help in my database, from there I hope to return echo's such as "User already exists" or "Registration successful". I've been trying to read some documentation, but I'm unable to really find what I'm looking at. Though, I do feel as if 

$stmt->rowCount()

is my best bet for what i'm trying to accomplish. Could someone talk me through how it actually works and perhaps give a small pseudo example of how it actually works? Doesn't it kind of work by comparing submitted rows or something? Not entirely sure.

 

Thank you all for reading.

Tweezy

Link to comment
Share on other sites

To check if a user already exists with the username the user is inputting, simply execute a database query that SELECT's a record WHERE the username is what has been submitted. If you get a result, that username is already taken.

 

I don't see how that kind of logic would change between languages.

Link to comment
Share on other sites

To check if a user already exists with the username the user is inputting, simply execute a database query that SELECT's a record WHERE the username is what has been submitted. If you get a result, that username is already taken.

 

I don't see how that kind of logic would change between languages.

 

I've never touched any databases before doing php, so like I said i'm pretty new to it. This is what I have at the moment, doesn't seem to be working though:

 

if($stmt->rowCount() === 0 )
{
            $stmt = $conn->prepare("INSERT INTO users (username, password, email) VALUES (?,?,?)");
            $stmt->execute(array($username, $password, $email));
            if($stmt->rowCount() === 1)
{
                echo 'Registration complete'; 
            }
else
{
                echo 'Sorry, unknown error: please try again later';


            }


            }else
{
                echo 'Sorry, the username '.$username.' already exists';
            }
Link to comment
Share on other sites

  • Solution

Sorry. I was doing this at my laptop at work. I'll tell you what I did and how it worked.

 

Okay, to start I was basically comparing the Username, password and Email to see if they were already being used (That was my first mistake). After that I was checking for the rowCount() to see what was returned, well I was submitting three selections, so if my understanding is correct, I was selecting the username, password and email field. It would only result in "The user is already registered" when all three were in use. That was my second issue. To fix it, I just re-did my select query:

 

 
 
$stmt = $conn->prepare("SELECT username FROM users WHERE username = ? ");
$stmt->execute(array($username));


if($stmt->rowCount() === 0 )
{
     $stmt = $conn->prepare("INSERT INTO users (username, password, email) VALUES (?,?,?)");
     $stmt->execute(array($username, $password, $email));


               if($stmt->rowCount() === 1)
               {
                    echo 'Registration complete'; 
               }
               else
               {
                    echo 'Sorry, unknown error: Please try again later';
               }
     }
     else
     {
          echo 'Sorry, the username '.$username.' already exists';
      }
}

Quite a simple fix really. Hopefully this code will come in useful for other people here on the forums! Sorry about how long it took me to post this folks. 

Edited by Tweezy
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.