Jump to content

problems with duplicate data


yobo

Recommended Posts

Hey all,

 

My form adds the data to the mysql database fine however when the user hits the refresh button the same data is added again. how do I go about stopping this, I also a newbie so please bear with me.

 

<?php
include 'db.php';
include 'function.php'; 

if(!isset($_POST['register'])) { 



} else {

//init error variables
$errorusername = false;
$erroremail = false;
$erroremailconf = false;
$errorpassword = false;
$errorpassconf = false;

$username = isset($_POST['username']) ? trim($_POST['username']) : '';
$email = isset($_POST['email']) ? trim($_POST['email']) : '';
$emailconf = isset($_POST['emailconf']) ? trim($_POST['emailconf']) : '';
$password = isset($_POST['password']) ? trim($_POST['password']) : '';
$passconf = isset($_POST['passconf']) ? trim($_POST['passconf']) : '';

if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) $erroremail = true;
if(strlen($username) == '0') $errorusername = true;
if(strlen($emailconf) == '0') $erroremailconf = true;
if(strlen($password) == '0') $errorpassword = true;
if(strlen($passconf) =='0') $errorpassconf = true;

//display form again
if($errorusername || $erroremail || $erroremailconf || $errorpassword || $errorpassconf) {
	showForm($errorusername,$erroremail,$erroremailconf,$errorpassword,$errorpassconf);

} else {

	//check user email

	$query = mysql_query("SELECT email FROM users WHERE email = '".$email."'");
	if(@mysql_query($query)) $erroremail = true;

			$query2 = "INSERT INTO users SET
				username='$username',
				password='$password',
				email='$email'";


	if(@mysql_query($query2)) {
		echo 'Signup Completed';
		}else{
			echo 'failed to signup';
			}	




	}


}
?>

 

thanks

 

Joe

Link to comment
https://forums.phpfreaks.com/topic/128010-problems-with-duplicate-data/
Share on other sites

You already did it, but in a wrong order!

 

Here is a fix:

 

<?php
include 'db.php';
include 'function.php'; 

if($_POST['register']) { 

   
   //init error variables
   $errorusername = false;
   $erroremail = false;
   $erroremailconf = false;
   $errorpassword = false;
   $errorpassconf = false;
   
   $username = isset($_POST['username']) ? trim($_POST['username']) : '';
   $email = isset($_POST['email']) ? trim($_POST['email']) : '';
   $emailconf = isset($_POST['emailconf']) ? trim($_POST['emailconf']) : '';
   $password = isset($_POST['password']) ? trim($_POST['password']) : '';
   $passconf = isset($_POST['passconf']) ? trim($_POST['passconf']) : '';
   
   if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) $erroremail = true;
   if(strlen($username) == '0') $errorusername = true;
   if(strlen($emailconf) == '0') $erroremailconf = true;
   if(strlen($password) == '0') $errorpassword = true;
   if(strlen($passconf) =='0') $errorpassconf = true;
   
   //display form again
   if($errorusername || $erroremail || $erroremailconf || $errorpassword || $errorpassconf) {
      showForm($errorusername,$erroremail,$erroremailconf,$errorpassword,$errorpassconf);
   
   } else {
      
      //check user email
      
      $query = mysql_query("SELECT email FROM users WHERE email = '".$email."'");
      if(@mysql_query($query)) $erroremail = true;
      
            $query2 = "INSERT INTO users SET
               username='$username',
               password='$password',
               email='$email'";
               
               
      if(@mysql_query($query2)) {
         echo 'Signup Completed';
         }else{
            echo 'failed to signup';
            }   
           
      }

} else {

echo "Nothing happens!"; //Leave this empty, I just putted it for test

}
?>

OK, maybe you can do this:

 

$query = mysql_query("SELECT email FROM users WHERE email = '".$email."'");
while($row=mysql_fetch_array($query)){
$email1 = $row["email"];
}

if ($email1 == $email){
      if(@mysql_query($query)) $erroremail = true;
      
            $query2 = "INSERT INTO users SET
               username='$username',
               password='$password',
               email='$email'";

} else {

echo "Data is already in DB!";

}

[/code]

 

Here is antoher check, where we will check if that email is already stored in DB and if it is it will display an error message. Maybe you will need to adjust a code a litlle bit.

There are two ways to do this.

 

If something about the data is unique, such as an email address, then you can let the database do this by making that column a unique index. Attempting to insert the same value will fail, which you can detect in your logic (or you can perform a select query to see if the value is already in the database, which is what I believe the existing code is/was attempting to do.)

 

To use a session to prevent the data from being operated on more than once, you set a session variable in the form processing code that says that the data has been processed and then in the code at the start of the page you check if that session variable is already set and skip over the form processing code if it is.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.