Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8271597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:53:22+00:00 2026-06-08T06:53:22+00:00

I have the following code which updates my database with the userID, value (liked

  • 0

I have the following code which updates my database with the userID, value (liked or disliked), and reminder date whenever a user ‘likes’ a contest (which is a post).

$query looks at my userContests table and stores it’s entire contents.

The userContests table stores the userID, value (liked or disliked), contestID, and reminder date.

$getfreq is an array containing values 123, 234, 345, 456, 567

I hope the code explains itself, I’ve done my best to comment each section for you. The basic point here is that if $getfreq contains value 345, insert or update my database with the reminder date.

$userID = 1;
$value = 1;
$contestID = 1737;
if (($userID > 0) && ($contestID > 0) && ($value < 2)){
    $query = mysql_query("SELECT * FROM userContests WHERE userID='$userID' AND contestID='$contestID';") or die(mysql_error()); 

    $getfreq = mysql_query("
            SELECT wp_term_relationships.term_taxonomy_id 
            FROM wp_term_relationships 
            WHERE wp_term_relationships.object_id = $contestID
        "); 

    while ($row = mysql_fetch_assoc($getfreq)) {
        if ($value == 1){ // If the contest is liked
            if (mysql_num_rows($query) > 0) {  //if a value matching the userID and contest ID already exists in database
                 if ($row[term_taxonomy_id] == 345) { // if Daily entry, update the row with reminder date
                    echo "1";
                    mysql_query("UPDATE userContests SET value='$value', reminder= DATE_ADD(CURDATE(),INTERVAL 1 DAY) WHERE userID='$userID' AND contestID='$contestID';") or die(mysql_error());
                    $frequency = 'Daily';
                } else { // if anything other than above, insert the row with current date
                    echo "2";
                    mysql_query("UPDATE userContests SET value='$value', reminder= CURDATE() WHERE userID='$userID' AND contestID='$contestID';") or die(mysql_error());
                }
            } else { // if there is no previous row in database matching userID and contestID
                if ($row[term_taxonomy_id] == 345) { // if Daily entry, insert the row with reminder date
                    echo "3";
                    mysql_query("INSERT INTO userContests (userID, contestID, value, reminder) VALUES ('$userID', '$contestID', '$value', 'DATE_ADD(CURDATE(),INTERVAL 1 DAY)') ") or die(mysql_error());
                } else { // if anything other than above, insert the row with current date
                    echo "4";
                    mysql_query("INSERT INTO userContests (userID, contestID, value, reminder) VALUES ('$userID', '$contestID', '$value', CURDATE()) ") or die(mysql_error());
                }
            }
        } else if ($value == 0){ // if the value is disliked
            if (mysql_num_rows($query) > 0) {  //if a value matching the userID and contest ID already exists in database, simply update the row without reminder
                echo "5";
                mysql_query("UPDATE userContests SET value='$value' WHERE userID='$userID' AND contestID='$contestID';") or die(mysql_error());
            } else { // if there is no previous row in database matching userID and contestID, simply insert the row without reminder
            echo "6";
                mysql_query("INSERT INTO userContests (userID, contestID, value) VALUES ('$userID', '$contestID', '$value') ") or die(mysql_error());
            }
        }
    }
}

My code works. Sort of. My problem is that the insert code is inserting itself in my database 5 times. I only want it to insert once.

The echoes in the code serve for debugging. I am receiving 4 4 3 4 4 as echoes.

I know the problem is because of the loop, but I don’t know how to solve the problem.

I’ve deduced that the reason it is inserting the code 5 times is because as the code loops, it checks to see if $row = 345. Since the first two and last two times it doesn’t, the code inserts as per echo 4 while ont he 3rd loop, it inserts as per echo 3 since there is a match.

Now, I am aware of in_array() but don’t know how to use it in the context of my mysql query. I suspect that this might be the solution…

Can anyone help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:53:23+00:00Added an answer on June 8, 2026 at 6:53 am

    I don’t think you have an error in the loop.

    You just never update any of the variables, that you have in the inserting SQL.

    I mean – you say you have 5 values in $getfreq, the third one is 345 and therefore the branch with “3” will happen, in all the other cases, the branch with “4” will happen. None of the other variables are changed, ever.

    You get into this branch

               if ($row[term_taxonomy_id] == 345) { // if Daily entry, insert the row with reminder date
                    echo "3";
                    mysql_query("INSERT INTO userContests (userID, contestID, value, reminder) VALUES ('$userID', '$contestID', '$value', 'DATE_ADD(CURDATE(),INTERVAL 1 DAY)') ") or die(mysql_error());
                } else { // if anything other than above, insert the row with current date
                    echo "4";
                    mysql_query("INSERT INTO userContests (userID, contestID, value, reminder) VALUES ('$userID', '$contestID', '$value', CURDATE()) ") or die(mysql_error());
                }
    

    In the third case, it gets into the “3” branch and is inserted.

    However, in all the other cases, this line is executed

                    mysql_query("INSERT INTO userContests (userID, contestID, value, reminder) VALUES ('$userID', '$contestID', '$value', CURDATE()) ") or die(mysql_error());
    

    because you have it in else branch.

    If you don’t want to do the loop at all and just want to check once, you will have to do something like this.

    $getfreq = mysql_query("
            SELECT COUNT(taxonomy_id) AS freq
            FROM wp_term_relationships 
            WHERE wp_term_relationships.object_id = $contestID
             AND wp_term_relationshis.taxonomy_id = 345
        ");    
    
    $data = mysql_fetch_assoc($getfreq);
    
    if ($data['freq'] > 1) {
        //do something
    } else {
        //do something else
    }
    

    I hope I wrote it correctly since I am doig it from top of my head 🙂

    If you want to check for more than one value, try this:

    $getfreq = mysql_query("
            SELECT DISTINCT term_taxonomy_id
            FROM wp_term_relationships 
            WHERE wp_term_relationships.object_id = $contestID
        ");    
    
    while ($row = mysql_fetch_assoc($getfreq)) {
        $taxonomy_id = $row[term_taxonomy_id];
        $seen[$taxonomy_id] = 1;
    }
    
    if ($seen[334]) {
       //something
    } else if ($seen[456]) {
       //something else
    } else {
       //last branch
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code, which basically takes values from a database and populates
I have following code which works for radio buttons but need to be changed
I want to know is below code correct ? I have following code which
I have the following code which definitely returns a proper data result if I
I have the following code which is working, I was wondering if this can
I have the following code which is used to upload large files (~6MB) to
i have the following code which switches some fullscreen-background-images (fadeOut, fadeIn). setInterval(function() { var
I have the following code which is fine if I give invalid parameters (though,
I have the following code which will generate two pdf files containing plots to
I have the following code which utilises Guava's Files.readLines() method: List<String> strings = Lists.newArrayList();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.