justlukeyou Posted February 26, 2013 Share Posted February 26, 2013 Hi,I am trying to come up with a way of stopping duplicate inserts from being into a table. I have got this point which creates an error message for duplicate follows but I am stuck on what to try next. Any suggestions please? As far as I can tell I need something in the insert code that says if the value is already in the table it cannot be re-inserted and then echo the message. $query = "INSERT INTO `follow` (`user_id`, `follow_user_id`) VALUES ('{$profileid}', '{$followerid}')"; $result = mysql_query($query); if (!$result){ $errors[] = "Query: {$query}<br>Error: " . mysql_error(); } } if(($profileid) == ($followerid)) { $errors['sameprofile'] = "You cannot follow your own profile."; } $loginprofile = intval($_SESSION['userID']); $query_rsSearch = "SELECT * FROM follow WHERE `follow_user_id` = '$loginprofile'"; $rsSearch = mysql_query($query_rsSearch) or die(mysql_error()); $row_rsSearch = mysql_fetch_assoc($rsSearch); $totalRows_rsSearch = mysql_num_rows($rsSearch); $duplicate = $profileid; if(($loginprofile) == $row_rsSearch['follow_user_id']) { $errors['duplicatefollow'] = "You already follow this profile."; } I have this across a number of pages so I should be able to re-use it quite often. Quote Link to comment https://forums.phpfreaks.com/topic/274991-stopping-duplicate-inserts/ Share on other sites More sharing options...
justlukeyou Posted February 27, 2013 Author Share Posted February 27, 2013 Hi, I have added this which returns the error message however I cant work out how to stop the duplicate input. Can anyone who knows PHP able to advise how to stop the PHP insert once the error message runs? $query = mysql_query("SELECT * FROM follow WHERE follow_user_id = '". $followerid ."'"); $duplicatefollow = null; if (mysql_num_rows($query) > 0) { $duplicatefollow = 'You Already Follow This Profile.</a>.'; } if(isset($_POST['followbutton'])) { $query = "INSERT INTO `follow` (`user_id`, `follow_user_id`) VALUES ('{$profileid}', '{$followerid}')"; $result = mysql_query($query); if (!$result){ $errors[] = "Query: {$query}<br>Error: " . mysql_error(); } } Quote Link to comment https://forums.phpfreaks.com/topic/274991-stopping-duplicate-inserts/#findComment-1415448 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.