yobo Posted October 11, 2008 Share Posted October 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/128010-problems-with-duplicate-data/ Share on other sites More sharing options...
budimir Posted October 11, 2008 Share Posted October 11, 2008 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/128010-problems-with-duplicate-data/#findComment-662920 Share on other sites More sharing options...
yobo Posted October 12, 2008 Author Share Posted October 12, 2008 thanks for helping m8 but it still dont work, some people have said use sessions but how do i use them when submitting form data?, i only need the data to be added to the database once not every time the user hits the reffresh button on the broswer Quote Link to comment https://forums.phpfreaks.com/topic/128010-problems-with-duplicate-data/#findComment-663394 Share on other sites More sharing options...
budimir Posted October 12, 2008 Share Posted October 12, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/128010-problems-with-duplicate-data/#findComment-663397 Share on other sites More sharing options...
PFMaBiSmAd Posted October 12, 2008 Share Posted October 12, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/128010-problems-with-duplicate-data/#findComment-663398 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.