|
| |
|
|
Ryokotsusai
Posts: 248 Joined: 10/5/2005 Status: offline
|
*SOLVED* Check Against Records (MySQL) - 4/23/2006 14:09:34
Hi, is there a simple way of comparing a user input to entries in a database and stopping them if they match? such as [forgive me if im way off with this] if ( $uname == *info from db* ) {
die ( "Username is already in use" );
} else {
echo "Username checks out";
} how would you compare it to all of the like entries in the db?
< Message edited by Ryokotsusai -- 4/24/2006 13:21:14 >
_____________________________
The world is more like it is now than it ever has been before. --Dwight Eisenhower
|
|
|
|
yb2
Posts: 653 Joined: 1/30/2006 Status: offline
|
RE: Check Against Records (MySQL) - 4/23/2006 15:15:49
quote:
is there a simple way of comparing a user input to entries in a database and stopping them if they match? put the conditional statement into a function, and when it gets to the bit you want it to stop at use the RETURN keyword, or use a WHILE loop, and have it do the loop while $uname == whatever is not true. quote:
how would you compare it to all of the like entries in the db? The first method you've implied you're using (above) would mean you'd taken all the data out of the database, probably put it into an array and would loop through and check it record by record. If you wanted to check 1 particular value against all the database entries, then you would use the database engine to do it instead of PHP since databases are optimised for that, with a SQL statement and probably IF EXISTS CREATE PROCEDURE CheckUserNameAlreadyExists_usp ( IN Username VARCHAR(120), OUT myretval BOOLEAN ) SET myretval = false; IF EXISTS ( SELECT name FROM Table WHERE name = $uname ) BEGIN SET myretval = true; END you'd need to be using mySQL 5 to use this, put it into a stored procedure and call it from your PHP. I don't use mySQL much so you will need to check the syntax out properly.
_____________________________
it is natural for people not to see one's own faults, and to exaggerate other people's faults and failings. Currently listening to: L'Enfer Des Formes by Stereolab
|
|
|
|
Ryokotsusai
Posts: 248 Joined: 10/5/2005 Status: offline
|
RE: Check Against Records (MySQL) - 4/23/2006 22:26:51
How do you store a procedure in MySQL? Edit>> and it looks like i only have mysql 4 something
< Message edited by Ryokotsusai -- 4/23/2006 22:35:45 >
_____________________________
The world is more like it is now than it ever has been before. --Dwight Eisenhower
|
|
|
|
Ryokotsusai
Posts: 248 Joined: 10/5/2005 Status: offline
|
RE: Check Against Records (MySQL) - 4/24/2006 11:00:07
Ok, this is what i have so far, but it always tells me that the username checks out, even when it shouldn't <?
//Connection Settings
$sqname="************";
$sqpass="********";
$sqdata="************";
mysql_connect("12.34.56.78",$sqname,$sqpass);
@mysql_select_db($sqdata) or die("Unable to select database");
//Data From Html
$fname = $_POST['FN'];
$lname = $_POST['LN'];
$uname = $_POST['UN'];
$upass = $_POST['PASS'];
$cpass = $_POST['PASS2'];
$email = $_POST['EM'];
$url = $_POST['URL'];
$aim = $_POST['AIM'];
$msn = $_POST['MSN'];
$yah = $_POST['YAH'];
$not = $_POST['NOT'];
//Functions
function CheckUserNameAlreadyExists( $uname ) {
//build the sql statement
//it gets the number of times the username is in the database
//if greater than 0 then it's in there
$sql = sprintf("SELECT COUNT(*) AS test FROM Users WHERE Username = %s ", $uname);
//connect to the database
$conn = mysql_connect("65.98.98.58",$sqname,$sqpass);
@mysql_select_db($sqdata);
$result = mysql_query($sql);
//check the result
if ( $result >= 1 )
{
$message = die("That username is already in use!");
}
else
{
$message = "That username checks out<br>";
}
//output the result
return $message;
}//end of function
//Check Username
echo CheckUserNameAlreadyExists( $uname );
//The posting
$post = "INSERT INTO Users VALUES ('$fname','$lname','$uname','$upass','$email','$url','$aim','$msn','$yah','$not')";
mysql_query($post);
//Ends Connection
mysql_close();
?> am i not supposed to use a sprintf() there?
< Message edited by Ryokotsusai -- 4/24/2006 13:40:58 >
_____________________________
The world is more like it is now than it ever has been before. --Dwight Eisenhower
|
|
|
|
Ryokotsusai
Posts: 248 Joined: 10/5/2005 Status: offline
|
*SOLVED* Check Against Records (MySQL) - 4/24/2006 13:20:48
*SOLVED* ok, after playing with things and reading a few tutorials, it finaly worked! the code right now for anyone who cares is:<?
//Connection Settings
$sqname="*******_dotm";
$sqpass="********";
$sqdata="*******_dotm";
mysql_connect("12.34.56.78",$sqname,$sqpass);
@mysql_select_db($sqdata) or die("Unable to select database");
//Data From Html
$fname = $_POST['FN'];
$lname = $_POST['LN'];
$uname = $_POST['UN'];
$upass = $_POST['PASS'];
$cpass = $_POST['PASS2'];
$email = $_POST['EM'];
$url = $_POST['URL'];
$aim = $_POST['AIM'];
$msn = $_POST['MSN'];
$yah = $_POST['YAH'];
$not = $_POST['NOT'];
//Functions
function CheckUserNameAlreadyExists( $uname ) {
//build the sql statement
//it gets the number of times the username is in the database
//if greater than 0 then it's in there
$sql = sprintf("SELECT COUNT(*) AS test FROM Users WHERE Username = '%s'", $this->uname);
//connect to the database
$result = mysql_query($sql);
//check the result
if ( $result >= 1 )
{
$message = die("That username is already in use!");
}
else
{
$message = "That username checks out<br>";
}
//output the result
return $message;
}//end of function
//Check Username
echo CheckUserNameAlreadyExists( $uname );
//The posting
$post = "INSERT INTO Users VALUES ('$fname','$lname','$uname','$upass','$email','$url','$aim','$msn','$yah','$not')";
mysql_query($post);
//Ends Connection
mysql_close();
?>
< Message edited by Ryokotsusai -- 4/24/2006 13:40:24 >
_____________________________
The world is more like it is now than it ever has been before. --Dwight Eisenhower
|
|
|
|
yb2
Posts: 653 Joined: 1/30/2006 Status: offline
|
RE: *SOLVED* Check Against Records (MySQL) - 4/24/2006 13:37:28
Glad to see it works, but if that's a real IP address in your code I'd take it out from your (public) post - no need to encourage those pesky crackers with any details about your database's existence!
_____________________________
it is natural for people not to see one's own faults, and to exaggerate other people's faults and failings. Currently listening to: L'Enfer Des Formes by Stereolab
|
|
New Messages |
No New Messages |
Hot Topic w/ New Messages |
Hot Topic w/o New Messages |
Locked w/ New Messages |
Locked w/o New Messages |
|
Post New Thread
Reply to Message
Post New Poll
Submit Vote
Delete My Own Post
Delete My Own Thread
Rate Posts
|
|
|