I'm creating an upvote system on my site. Currently, the upvotes are working but I want to limit the upvotes to 1 per user. The votes are added by updating the total votes:
mysqli_query($connection,"UPDATE news SET votes=(votes + 1) WHERE ID='". $_POST['id1'] . "'");
And then I record that the user voted on that article by using:
mysqli_query($link,"UPDATE users SET votes=CONCAT(votes,'" . $_POST['id1'] .",') WHERE ID='". $_SESSION['ID'] . "'");
Now, I try to implement only allowing an upvote if the article id is not already in their "votes" database. I'm storing votes like 1,4,7,12
so that user voted on articles 1, 4, 7 and 12. So I'm trying to use explode to check if they already voted on an article:
$results = mysqli_query($link2,"SELECT * FROM users WHERE ID ='" . $_SESSION['ID'] . "'");
while($result = mysqli_fetch_array( $results )){
$votes = explode(",", $result['votes']);
foreach($votes as $vote) {
if ($vote = $_POST['id1']) {
//Do nothing
} else {
mysqli_query($connection,"UPDATE news SET votes=(votes + 1) WHERE ID='". $_POST['id1'] . "'");
mysqli_query($link,"UPDATE users SET votes=CONCAT(votes,'" . $_POST['id1'] .",') WHERE ID='". $_SESSION['ID'] . "'");
Any help or suggestions on a better way to accomplish the same thing would be greatly appreciated, thanks!