PDA

View Full Version : Need Helping Validating Submissions...


Jedi Josh
08-15-2003, 12:46 AM
So I won't have duplicate "usernames" in my database. I've tried for about an hour and a half now to add such functionality to this script, but I can't. Check out the whole thing.

<?
$usr = "jedijosh";
$pwd = "";
$db = "memberdb";
$host = "localhost";

// connect to database
$cid = mysql_connect($host,$usr,$pwd);
mysql_select_db($db);

?>
<html>
<head>
<title>Member Registration</title>

</head>
<body bgcolor="#ffffff">

<h2> Add Member </h2>

<?

if ($_SERVER['REQUEST_METHOD'] == "POST")
{

$username = addslashes($_POST["username"]);
$password = addslashes($_POST["password"]);
$email = addslashes($_POST["email"]);
$password = MD5($password);


$sql = " INSERT INTO members (username, password, email) VALUES ('$username', '$password','$email') ";


$result = mysql_query($sql, $cid);


print "<p><b>New member added</b></p>\n";
print "<a href='http://localhost/memberlist.php'>View Data</a>\n";

}

?>

<form name="submit" action="register.php" method="POST">
<table>
<tr><td><b>Name: </b> </td><td><input type="text" name="username" size=40></td></tr>
<tr><td><b>Password:</b> </td><td><input type="text" name="password" size=40></td></tr>
<tr><td><b>E-mail: </b> </td><td><input type="text" name="email" size=40></td></tr>

<tr><th colspan=2><p><input type="submit" value="Register"></p></th></tr>
</table>
</form>

</body>
</html>


Basically, if a user submits a username which is already taken, the script should print something like "taken". Any help guys? http://forums.massassi.net/html/biggrin.gif

------------------
To think that once I could not see beyond the veil of reality, to see those who dwell behind. I was once a fool.

lordvader
08-15-2003, 02:04 AM
Run the MySQL query:

SELECT username FROM members WHERE username = "$username"

Remember to escape the quotes when you put in PHP. Then, if there are any rows returned, that indicates that the username is taken.

------------------
Capitalization (http://webster.commnet.edu/grammar/capitals.htm)
Commas (http://webster.commnet.edu/grammar/commas.htm)
Periods (http://webster.commnet.edu/grammar/marks/period.htm)
Question Marks (http://webster.commnet.edu/grammar/marks/question.htm)
Apostrophes (http://webster.commnet.edu/grammar/marks/apostrophe.htm)
Confusable Words (http://webster.commnet.edu/grammar/notorious.htm)
Plague Words (http://webster.commnet.edu/grammar/plague.htm)

Jedi Josh
08-15-2003, 02:22 AM
Yeah I know how to do that, but I can't put it to good use. Do I put the main block of code in an IF statement?

I've been trying forever now and still can't figure it out.

I'm still new at this so I'm not too knowledgable http://forums.massassi.net/html/smile.gif

------------------
To think that once I could not see beyond the veil of reality, to see those who dwell behind. I was once a fool.

Jedi Josh
08-15-2003, 08:16 PM
C'mon someone has to know.

------------------
To think that once I could not see beyond the veil of reality, to see those who dwell behind. I was once a fool.

Pommy
08-15-2003, 08:56 PM
<?

if ($_SERVER['REQUEST_METHOD'] == "POST")

{

$username = addslashes($_POST["username"]);

$password = addslashes($_POST["password"]);

$email = addslashes($_POST["email"]);

$password = MD5($password);

$sql = "SELECT username FROM members WHERE username = "$username";

$ins = " INSERT INTO members (username, password, email) VALUES ('$username', '$password','$email') ";

$result = mysql_query($sql, $cid);

if (mysql_num_rows($result) > 1) {
die "Taken.";
} else {
mysql_query($ins);
print "<p><b>New member added</b></p>\n";

}

print "<a href='http://localhost/memberlist.php'>View Data</a>\n";

}

?>

------------------
Sigs are for n00bs.

[1337 FRNDS_Pommy (http://forums.massassi.net/cgi-bin/ubbmisc.cgi?action=getbio&UserName=FRNDS_Pommy) | 3.14 of 14 | » And-GTx2 (http://andgtx2.el3ctro.co.uk)]
Half-Life 2 Central (http://www.hl2central.net) - your definitive source for everything HL2!

[This message has been edited by Pommy (edited August 16, 2003).]

Jedi Josh
08-15-2003, 09:18 PM
"Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\FoxServ\www\register.php on line 38"

Err!

------------------
To think that once I could not see beyond the veil of reality, to see those who dwell behind. I was once a fool.

Jedi Josh
08-16-2003, 01:52 PM
So does anyone know how to fix the problem above?! http://forums.massassi.net/html/smile.gif

------------------
To think that once I could not see beyond the veil of reality, to see those who dwell behind. I was once a fool.

Pommy
08-16-2003, 02:31 PM
My bad, I forgot to change something. I fixed it with an edit.

------------------
Sigs are for n00bs.

[1337 FRNDS_Pommy (http://forums.massassi.net/cgi-bin/ubbmisc.cgi?action=getbio&UserName=FRNDS_Pommy) | 3.14 of 14 | » And-GTx2 (http://andgtx2.el3ctro.co.uk)]
Half-Life 2 Central (http://www.hl2central.net) - your definitive source for everything HL2!

Jedi Josh
08-16-2003, 06:58 PM
Thanks alot Pommy. Appreciate it http://forums.massassi.net/html/smile.gif.

------------------
To think that once I could not see beyond the veil of reality, to see those who dwell behind. I was once a fool.