im struggling with my code to update mysql entries using CRON. Maybe I just cant see what Im doing wrong, but maaan.. my head already hurts from overthinking this.
*This code should update table what shows up rounds for every user. I'd like it to add one round per 10mins for every user.
this is code, that is executed every 10mins:
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('universe_domination');
$sql = mysql_query("SELECT * FROM rounds");
foreach (mysql_fetch_array($sql) as $row) {
$id = $row['id'];
$free = $row['free'];
$saved = $row['saved'];
$lost = $row['lost'];
if ($free < 200) {
$roundSql = "UPDATE rounds SET free = free + 1 WHERE id = " . $id;
} elseif ($free == 200 && $saved < 300) {
$roundSql = "UPDATE rounds SET saved = saved + 1 WHERE id = " . $id;
} elseif ($free == 200 && $saved == 300) {
$roundSql = "UPDATE rounds SET lost = lost + 1 WHERE id = " . $id;
}
mysql_query($roundSql);
}
problem is, that now i have 4 entries.
id free saved lost
1 2 300 19568
2 200 250 19568
3 6 300 19568
4 7 300 19568
but after update it looks like this:
id free saved lost
1 6 300 19568
2 200 250 19568
3 6 300 19568
4 9 300 19568
Can somebody tell me whats wrong in that code? I believe that it is some beginners mistake, but i cannot find whats wrong
**So far it edits it somehow wrong... one row adds 4 rounds, third none and fourth adds 2 rounds
****Still no luck after using this:
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$sql = "UPDATE rounds SET free = free + (CASE WHEN free < 200 THEN 1 ELSE 0 END),
SET saved = saved + (CASE WHEN saved < 300 THEN 1 ELSE 0 END),
SET lost = lost + (CASE WHEN free = 200 AND saved = 300 THEN 1 ELSE 0 END)";
$updateRound = $conn->prepare($sql);
$updateRound->execute();
--table is not updated at all