I have problem with while loop and can't get code work like I want. My goal is when admin delete topic it check all users who are voted in that topic and then updates users that are voted (minus counts of specific user have voted topic).
So example I have Topic "Holidays"
These users have voted that topic
| Userid | Topic id votes | Total votes |
|---|---|---|
| Userid 1 | Voted "Holiday" topic 5 times | Total votes 400 |
| Userid 2 | Voted "Holiday" topic 8 times | Total votes 200 |
| Userid 3 | Voted "Holiday" topic 2 times | Total votes 80 |
And when I delete the topic it updates (check user ids that have voted and update their total votes)
These users have voted that topic
| Userid | Topic id votes | Total votes |
|---|---|---|
| Userid 1 | Voted "Holiday" topic 5 times | Total votes 395 |
| Userid 2 | Voted "Holiday" topic 8 times | Total votes 192 |
| Userid 3 | Voted "Holiday" topic 2 times | Total votes 78 |
My problem is code not working, or it works only first row (result). How I can get all users who are voted topic and after delete topic update (total votes - voted topic votes) for all users?
$topicid = (int)$_POST["topicid"];
$topics_res = mysql_query("SELECT userid FROM topics WHERE topicid = $topicid");
while($row_topic = mysql_fetch_array($topics_res)) {
$topic_userid = (int)$row_topic["userid"];
$res_topic_votes_count = mysql_query("SELECT userid, votes FROM topics WHERE topicid = $topicid AND userid = $topic_userid");
while($votes_count = mysql_fetch_array($res_topic_votes_count)) {
mysql_query("UPDATE users SET votes=votes-$votes_count[votes] WHERE id = $votes_count[userid]");
}
}
Topic structure:
`topics` (
`topicid` int(10) unsigned NOT NULL,
`userid` int(10) unsigned NOT NULL DEFAULT '0',
`votes` bigint(20) unsigned NOT NULL DEFAULT '0'
) ENGINE=MyISAM AUTO_INCREMENT=50 DEFAULT CHARSET=utf8;
Users structure:
`users` (
`id` int(10) unsigned NOT NULL,
`name` varchar(70) DEFAULT NULL,
`lastname` varchar(70) DEFAULT NULL,
`votes` bigint(20) unsigned NOT NULL DEFAULT '0'
) ENGINE=MyISAM AUTO_INCREMENT=50 DEFAULT CHARSET=utf8;