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?
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
In the third case, it gets into the “3” branch and is inserted.
However, in all the other cases, this line is executed
because you have it in
elsebranch.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.
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: