/    Sign up×
Community /Pin to ProfileBookmark

Crosscheck with database

Hey im fairly new with php, im steadily climbing the hill.

However i’ve come accross something which i can’t find the answer for, i need to know how i check something matches something on the database…

For example, i have a form, which someone is required to put in a Username, how do i do it, so the code checks to see if the username is on the database?

thanks in advance

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@phpshift2Feb 12.2007 — Well, assuming you already connected, you'd do something like this:
[code=php]mysql_query("SELECT * FROM TABLE_NAME WHERE username='".mysql_real_escape_string($_POST[username])."' ");
if(mysql_affected_rows()>0)
DO SOMETHING...[/code]
Copy linkTweet thisAlerts:
@DistantUKauthorFeb 12.2007 — Thank you for the speedy reply

Does

mysql_query("SELECT * FROM TABLE_NAME WHERE username='".mysql_real_escape_string($_POST[username])."' ");

Try and take what username was posted and try and match it with a name on the database and if so....

if(mysql_affected_rows()>0)

What does this do??
Copy linkTweet thisAlerts:
@phpshift2Feb 12.2007 — Thank you for the speedy reply

Does

mysql_query("SELECT * FROM TABLE_NAME WHERE username='".mysql_real_escape_string($_POST[username])."' ");

Try and take what username was posted and try and match it with a name on the database and if so....

if(mysql_affected_rows()>0)

What does this do??[/QUOTE]


Once that query is run, it will count how many rows of your database were affected, meaning they returned results. The number can be found in the mysql_affected_rows() function. If there are username matches in your database, the row count will be greater than one. The script asks if it's more than one and does whatever you tell it to if it is. ?
Copy linkTweet thisAlerts:
@DistantUKauthorFeb 12.2007 — Ok i know how to go about it now,


mysql_query("SELECT * FROM busers WHERE Username='".mysql_real_escape_string($_POST[aeonto])."' ");

i used aeonto, as thats the name of the input field..

However I get this error when i use it

Notice: Use of undefined constant aeonto - assumed 'aeonto' in ### on line ##

how do i define it? I mean i thought its defined when someone puts something into the input box?
Copy linkTweet thisAlerts:
@phpshift2Feb 12.2007 — Try $_POST['aeonto'] instead of $_POST[aeonto]. Some servers are more picky than others.
Copy linkTweet thisAlerts:
@DistantUKauthorFeb 12.2007 — Excellent it works thank you.

Im going to be cheeky now though, what i've got set up requires the Username in the input to be case-sensitive in regards to the database entry, however atm, aslong as the username is there it will accept it...

How do i make the result case-sensitive?
Copy linkTweet thisAlerts:
@NightShift58Feb 12.2007 — The code you are using may work - for the moment. It is, however, dangerous code as it exposes you to so-called SQL Injection. Also, it doesn't check if any user input has been made.

Use something like this instead - over the long term, it'll save you loads of grief:[code=php]<?php
IF (!isset($_POST['username'])):
echo "Please enter your user name.";
ELSE :
$username = $_POST['username'];
IF (get_magic_quotes_gpc()) :
$username = stripslashes($username);
ENDIF;
$username = mysql_real_escape_string($username);

$sql = "SELECT * FROM TABLE_NAME WHERE BINARY username = '$username' LIMIT 1");
$qry = mysql_query($sql) or die("SQL Error: $sql<br>" . mysql_error());

IF (mysql_num_rows($qry) == 1) :
// user found
ELSE :
//user not found
ENDIF;
ENDIF;
?>[/code]
Copy linkTweet thisAlerts:
@DistantUKauthorFeb 12.2007 — MMm.. would that make the result case sensitive?
Copy linkTweet thisAlerts:
@NightShift58Feb 12.2007 — Yes, the keyword "BINARY" in the SELECT statement does that.
Copy linkTweet thisAlerts:
@DistantUKauthorFeb 12.2007 — Excellent, thank you both for your help, I very much apprietiate it ?
Copy linkTweet thisAlerts:
@NightShift58Feb 12.2007 — You're welcome!
×

Success!

Help @DistantUK spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...