dknight3
08-02-2006, 05:33 AM
hi i want to use mysql_numrows to see if the email address is already registered and not allow it to be regitered again.
so heres what i have and it says "mysql_numrows(): supplied argument is not a valid MySQL result resource"
function duplicateemail($email)
{
$result=mysql_query("SELECT * FROM users WHERE email=$email");
if(mysql_numrows($result)>0){
return false;
}else{
return true;
}
}
NogDog
08-02-2006, 05:56 AM
It's because MySQL could not process your query and returned an error instead of a resource ID.
$result=mysql_query("SELECT * FROM users WHERE email='$email'");
Which is why you should always check the result of your mysql_query() commands. A useful idiom for debugging:
$query = "SELECT * FROM users WHERE email=$email";
$result = mysql_query($query) or die("Query failed: $query - " . mysql_error());
dknight3
08-02-2006, 04:15 PM
but is there a way so i can make it instead of returning an error, return true??
so it will create the user, because if it has an error that means there are 0 rows right?
EDIT: hmm i might have gotten it to work but all i get is "go back"
Heres the source for my whole script, this is register.php (form validation)
<?php
require 'config.php';
mysql_connect(localhost,$username,$password);
mysql_select_db(dknight3_users) or die( "Unable to select database");
$_POST = array_map('strip_tags', $_POST);
if (strlen($_POST['username']) < 3){
if (strlen($_POST['username']) > 20){
if ($_POST['username'] !== NULL){
$_POST['username'] = $username;
}else{
$error = 'Please enter a desired username!';
}
}else{
$error = 'Username to long!';
}
}else{
$error = 'Username is to short!';
}
if (strlen($_POST['password']) < 3){
if (strlen($_POST['password']) > 20){
if ($_POST['password'] == $_POST['repassword']){
if ($_POST['password'] !== NULL){
$_POST['password'] = $password;
}else{
$error = 'Please enter a password!';
}
}else{
$error = 'Your passwords do not match!';
}
}else{
$error = 'Password to long!';
}
}else{
$error = 'Password is to short!';
}
if ($_POST['email'] == $_POST['reemail']){
if ($_POST['email'] !== NULL){
if (checkemail($_POST['email'])){
if (duplicateemail($_POST['email'])){
$_POST['email'] = $email;
}else{
$error = 'Your email address has already been registered!';
}
}else{
$error = 'Invalid email address!';
}
}else{
$error = 'Please enter an Email address!';
}
}else{
$error = 'Your emails do not match!';
}
if (ctype_alpha($_POST['firstname'])){
if (strlen($_POST['firstname']) > 2){
if (strlen($_POST['firstname']) < 20){
if ($_POST['firstname'] !== NULL){
$_POST['firstname'] = $firstname;
}else{
$error = 'Please enter your first name!';
}
}else{
$error = 'First name to long!';
}
}else{
$error = 'First name is to short!';
}
}else{
$error = 'First name contains invalid characters!';
}
if (ctype_alpha($_POST['lastname'])){
if (strlen($_POST['lastname']) < 20){
$_POST['lastname'] = $lastname;
}else{
$error = 'Last name to long!';
}
}else{
$error = 'last name contains invalid characters!';
}
if (strlen($_POST['age']) < 3){
if (ctype_digit($_POST['age'])){
$_POST['age'] = $age;
}else{
$error = 'Age is not valid!';
}
}else{
$error = 'Age is to long!';
}
if ($_POST['sex'] !== NULL){
$_POST['sex'] = $sex;
}else{
$error = 'Please select your sex!';
}
if (strlen($_POST['icq']) < 25){
$_POST['icq'] = $icq;
}else{
$error = 'icq is to long!';
}
if (strlen($_POST['msn']) < 25){
$_POST['msn'] = $msn;
}else{
$error = 'msn is to long!';
}
if (strlen($_POST['aim']) < 25){
$_POST['aim'] = $aim;
}else{
$error = 'aim is to long!';
}
if (strlen($_POST['yim']) < 25){
$_POST['yim'] = $yim;
}else{
$error = 'yim is to long!';
}
if (strlen($_POST['location']) < 50){
$_POST['location'] = $location;
}else{
$error = 'Location is to long!';
}
$_POST['website'] = $website;
if (strlen($_POST['about']) < 10000){
$_POST['about'] = $about;
}else{
$error = 'To much info about you, i didnt ask for your life story.';
}
if (strlen($_POST['hobbys']) < 10000){
$_POST['hobbys'] = $hobbys;
}else{
$error = 'You have way to many hobbies.';
}
if (strlen($_POST['additional']) < 10000){
$_POST['additional'] = $additional;
}else{
$error = 'To much additional info.';
}
if($error=NULL){
mysql_query("INSERT INTO users VALUES('','$username','$password','$email','$firstname','$lastname','$age','$sex','$icq','$msn','$ai m','$yim','$location','$website','$about','$hobbys','$additional')");
$to = $email;
$subject = 'Activate your' . $websitename . 'account!';
$message = 'Thank you for creating an account with' . $websitename . 'to activate your account click on the following link or copy and paste it into your browsers address bar.<br>' . $websiteurl . 'activate.php&email=' . $email;
$headers = 'From:' . $webmasteremail . "\r\n" .
'Reply-To:' . $webmasteremail . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers)){
echo'<center>Your account has been created successfully!<br>Check you email for instructions on how to activate your account.</center>';
}
}else{
echo $error . '<br><a href="javascript: history.go(-1)">Go Back</a>';
}
function checkemail($email)
{
$pos = strpos($email, '@');
if ($pos === false){
return false;
}
else
{
$pos = strpos($email, '.', $pos);
if ($pos === false){
return false;
}
else
{
list($user, $mailDomain) = split("@", $email);
if (myCheckDNSRR($mailDomain, "MX")) {
return true;
}
else
{
return false;
}
}
}
}
function myCheckDNSRR($hostName, $recType = '')
{
if(!empty($hostName)) {
if( $recType == '' ){
$recType = "MX";
}
exec("nslookup -type=$recType $hostName", $result);
foreach ($result as $line) {
if(eregi("^$hostName",$line)) {
return true;
}
}
return false;
}
return false;
}
function duplicateemail($email)
{
$query = "SELECT * FROM users WHERE email=$email";
if ($result = mysql_query($query)){
return false;
}else{
return true;
}
}
mysql_close();
?>
EDIT: oh yea you can see what happens here: http://www.freescripts.exofire.net/test/register.html
just submit that form.
NogDog
08-02-2006, 04:27 PM
No, it means there was a syntax error or invalid table/column name in your query. (Note the single quotes in red I added in the first part of my reply.) If the query was valid, you would have gotten a valid resource_id and then mysql_num_rows() would have returned a 0 if there were no matches or else the number of rows returned, NOT an error.
dknight3
08-02-2006, 04:29 PM
well all i get now is go back so i think it might work, the function is at the very bottom of my script.
NogDog
08-02-2006, 04:32 PM
if ($result = mysql_query($query) and mysql_num_rows($result)){
dknight3
08-02-2006, 04:45 PM
hmm.... still all i get is the go back link... i dont know why it would just display that...