Tweezy Posted July 17, 2013 Share Posted July 17, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/280242-php-comparing-inputting-data-against-a-database/ Share on other sites More sharing options...
trq Posted July 17, 2013 Share Posted July 17, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/280242-php-comparing-inputting-data-against-a-database/#findComment-1441075 Share on other sites More sharing options...
Tweezy Posted July 17, 2013 Author Share Posted July 17, 2013 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'; } Quote Link to comment https://forums.phpfreaks.com/topic/280242-php-comparing-inputting-data-against-a-database/#findComment-1441077 Share on other sites More sharing options...
Tweezy Posted July 17, 2013 Author Share Posted July 17, 2013 Ah nevermind, I managed to fix my issues. Quote Link to comment https://forums.phpfreaks.com/topic/280242-php-comparing-inputting-data-against-a-database/#findComment-1441082 Share on other sites More sharing options...
codelinx Posted July 17, 2013 Share Posted July 17, 2013 can you post what you fixed maybe to help others? Quote Link to comment https://forums.phpfreaks.com/topic/280242-php-comparing-inputting-data-against-a-database/#findComment-1441085 Share on other sites More sharing options...
codelinx Posted July 17, 2013 Share Posted July 17, 2013 i guess we are only about asking for help and not sharing or giving it ... Quote Link to comment https://forums.phpfreaks.com/topic/280242-php-comparing-inputting-data-against-a-database/#findComment-1441117 Share on other sites More sharing options...
Solution Tweezy Posted July 18, 2013 Author Solution Share Posted July 18, 2013 (edited) 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 July 18, 2013 by Tweezy Quote Link to comment https://forums.phpfreaks.com/topic/280242-php-comparing-inputting-data-against-a-database/#findComment-1441199 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.