Have an account? Sign in
Login  Register  Facebook
Duplicate record
The duplicate record check does not work properly

if($_POST) { 

	$required = array('full_name','usr_email','pwd','user_name');
	$missing = array();

	foreach($_POST as $key => $value) {
	
	if (empty($value) && in_array($key, $required)) {
            array_push($missing, $key);
        } else {
		${$key} = escape($value);
    }
}

if($_POST['usr_email'] !='') {

	if (!isEmail($_POST['usr_email'])) {
            array_push($missing, 'email_invalid');
        }

}

	$duplicate = "SELECT count(*) as total FROM users WHERE user_email='".$_POST['usr_email']."'";
	mysql_select_db($database_conndb, $conndb);
	$result = mysql_query($duplicate, $conndb) or die(mysql_error());
	$rows = mysql_fetch_assoc($result);
	$total_rows = mysql_num_rows($result);

	if ($total_rows > 0) {

		array_push($missing, 'duplicate');

	}

	mysql_free_result($result);


if (!empty($missing)) {

	if(in_array('full_name', $missing)) {
	
	$full_name = 'Please provide your full name';

	}


	if(in_array('usr_email', $missing)  || in_array('email_invalid', $missing)) {
	
	$usr_email = 'Please provide your valid email address';

	}


	if(in_array('duplicate', $missing)) {
	
	$usr_email = 'Email address already registered';

	}

	
	if(in_array('user_name', $missing)) {
	
	$user_name = 'Please create a password';

	}

	
	if(in_array('pwd', $missing)) {
	
	$pwd = 'Please create a password';

	}
		
} else {

// stores sha1 of password
$sha1pass = PwdHash(${'pwd'});

$usr_email = ${'usr_email'};
$user_name = ${'user_name'};


	$sql = "INSERT INTO users (`full_name`,`user_email`,`pwd`,`user_name`)
	VALUES ('${full_name}','${usr_email}','$sha1pass','${user_name}')";
	mysql_select_db($database_conndb, $conndb);
	$result = mysql_query($sql, $conndb) or die(mysql_error());
	
		$success ='<p>Thank you.<br />You have succesfully registered.</p>';
	}

}
<html>
<head>
<title>PHP Login :: Free Registration/Signup Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<link href="styles.css" rel="stylesheet" type="text/css">
</head>

<body>
    <?php if (isset($success)) { echo $success; } else { ?>
    <form action="" method="post" name="regform" id="regform">
      <table border="0" cellspacing="0" cellpadding="0">
     <?php if (isset($full_name)) { ?>
    <tr>
    <th> </th>
    <td class="warn"><?php echo $full_name; ?></td>          
    </tr>
    <?php } ?>
        <tr>
          <th scope="row"><label for="full_name">Full name</label></th>
          <td><input type="text" name="full_name" id="full_name"></td>
        </tr>
        <?php if (isset($user_name)) { ?>
    <tr>
    <th> </th>
    <td class="warn"><?php echo $user_name; ?></td>          
    </tr>
    <?php } ?>
        <tr>
          <th scope="row"><label for="user_name">User Name:</label></th>
          <td> <input type="text" name="user_name" id="user_name"></td>
        </tr>
        <?php if (isset($usr_email)) { ?>
    <tr>
    <th> </th>
    <td class="warn"><?php echo $usr_email; ?></td>          
    </tr>
    <?php } ?>
          <th scope="row"><label for="user_email">User Email:</label></th>
          <td> <input type="text" name="usr_email" id="usr_email"></td>
        </tr>
        <?php if (isset($pwd)) { ?>
    <tr>
    <th> </th>
    <td class="warn"><?php echo $pwd; ?></td>          
    </tr>
    <?php } ?>
        <tr>
          <th scope="row"><label for="pwd">Password</label></th>
          <td><input type="password" name="pwd" id="pwd"></td>
        </tr>
        <tr>
          <th scope="row"> </th>
          <td><input type="submit" name="button" id="button" value="Submit"></td>
        </tr>
      </table>
    </form>
    
     // <?php } ?>
     

</body>
</html>
When I click on submit button it gives error saying record already exists even on an empty database mysql
CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `full_name` tinytext COLLATE latin1_general_ci NOT NULL,
  `user_name` varchar(200) COLLATE latin1_general_ci NOT NULL DEFAULT '',
  `user_email` varchar(220) COLLATE latin1_general_ci NOT NULL DEFAULT '',
  `pwd` varchar(220) COLLATE latin1_general_ci NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_email` (`user_email`),
  FULLTEXT KEY `idx_search` (`full_name`,`user_email`,`user_name`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;

does anyone know why the dupliacte record check does not work?
Started: September 21, 2011 Latest Activity: September 21, 2011 php mysql validation duplicaterecord
1 Answer
try that
if($_POST) { 

    $required = array(\'full_name\',\'usr_email\',\'pwd\',\'user_name\');
    $missing = array();

    foreach($_POST as $key => $value) {
        if (empty($value) && in_array($key, $required)) {
                array_push($missing, $key);
            } else {
            $$key = escape($value);
        }
    }

    if($_POST[\'usr_email\'] !=\'\') {
        if (!isEmail($_POST[\'usr_email\'])) {
                array_push($missing, \'email_invalid\');
            }
    }

if (!empty($missing)) {

    if(in_array(\'full_name\', $missing)) {
    
    $full_name = \'Please provide your full name\';

    }


    if(in_array(\'usr_email\', $missing)  || in_array(\'email_invalid\', $missing)) {
    
    $usr_email = \'Please provide your valid email address\';

    }


    if(in_array(\'duplicate\', $missing)) {
    
    $usr_email = \'Email address already registered\';

    }

    
    if(in_array(\'user_name\', $missing)) {
    
    $user_name = \'Please create a password\';

    }

    
    if(in_array(\'pwd\', $missing)) {
    
    $pwd = \'Please create a password\';

    }
        
} else {   
        $duplicate = \"SELECT * FROM `users` WHERE `user_email` =\'$user_email\'\";
        if($dup = mysql_fetch_array(mysql_query($duplicate))){
            $success =\'<p>sorry.<br />there is a user use \'.$dup[\'user_email\'].\'.</p>\';
        }else{
            $sql = \"
            INSERT INTO users 
                (`full_name`,`user_email`,`pwd`,`user_name`)
                    VALUES 
                        (\'$full_name\',\'$user_email\',\'\".PwdHash($pwd).\"\',\'$user_name\')\";
            
                $success =\'<p>Thank you.<br />You have succesfully registered.</p>\';            
        }
    }
}

please to make a database connection in each page

Posted: MacOS
In: September 21, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer