Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/274991-stopping-duplicate-inserts/
Share on other sites

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();
    }
}       
 
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.