Kazhultee Posted July 27, 2009 Share Posted July 27, 2009 if ($_REQUEST['name'] != "") { $code = $_REQUEST['name']; $query="INSERT INTO `friends` (`name`) values ('" . mysql_real_escape_string($name) . "')"; $result=mysql_query($query) or die(mysql_error()); echo("Inserted " . htmlentities($name) ." into database"); } else { echo("<p>Please enter a name</p>"); } ?> -- the database has the unique function already so it knows not to allowmultiple entries that are the same. That's my code, how do I make it so that if that name is already there it refreshes page (shows everything still) but says taht that name was already used instead of saying "Duplicate entry 'frank' for key 1" So like right above where they wrote their name they get a message saying "name already added to list." thanks Quote Link to comment https://forums.phpfreaks.com/topic/167630-warning-users-of-duplicate-entry/ Share on other sites More sharing options...
JonnoTheDev Posted July 27, 2009 Share Posted July 27, 2009 You should ALWAYS reload the page after an insert or update query. This prevents duplicate entries being inserted or mysql errors if a user ever clicked refresh i.e if($_POST['submit']) { // insert record mysql_query("INSERT INTO ......"); // reload page header("Location:index.php"); exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/167630-warning-users-of-duplicate-entry/#findComment-884004 Share on other sites More sharing options...
Kazhultee Posted July 27, 2009 Author Share Posted July 27, 2009 Can you dumb thus down for me I am new. Also how do I print a message if there already is an entry like the one they submitted. Quote Link to comment https://forums.phpfreaks.com/topic/167630-warning-users-of-duplicate-entry/#findComment-884011 Share on other sites More sharing options...
JonnoTheDev Posted July 27, 2009 Share Posted July 27, 2009 I dont know how I can explain it easier. If you ever insert into a database or update a record always reload the page using the header() function after the query. This will prevent multiple records being inserted if the user was to click the refresh button in their browser (it will keep running over the same bit of code). You only run the queries when a user clicks the form submit button right? So if you reload the page the form is not in submit mode. To check if a name already exists then you would write a SELECT query prior to an insert query. See if the name exists from the SELECT query i.e. SELECT id FROM table WHERE name='neil'. If a record is returned then you throw an error and don't continue to insert the record. Quote Link to comment https://forums.phpfreaks.com/topic/167630-warning-users-of-duplicate-entry/#findComment-884019 Share on other sites More sharing options...
Kazhultee Posted July 27, 2009 Author Share Posted July 27, 2009 Okay, I think I understand but I'm not sure how to write the code. =[ Quote Link to comment https://forums.phpfreaks.com/topic/167630-warning-users-of-duplicate-entry/#findComment-884131 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.