cloudll Posted August 28, 2011 Share Posted August 28, 2011 Im not to sure what I have done wrong here, I have tried a few }'s in places incase I have missed one, im still quite new to pdo so im not sure. Can anyone see why it isnt updating the database? Cheers <?php if ($enemy_hp <= 0) { $sql = "UPDATE game_status SET battle = '1' WHERE id=1"; } else { $sql = "UPDATE game_character SET current_hp = (current_hp-100) WHERE id=1"; $statement = $dbh->prepare($sql); $statement->execute(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245915-not-getting-any-errors-but-its-not-updating-my-database/ Share on other sites More sharing options...
Pikachu2000 Posted August 28, 2011 Share Posted August 28, 2011 Prepared statements aren't really my "thing", but the query in the first part of your conditional won't be executed even if the condition is true; the code that executes the query is inside the else clause. Have you executed the queries directly in phpMyAdmin or the MySQL console to verify that they work as expected? if ($enemy_hp <= 0) { $sql = "UPDATE game_status SET battle = '1' WHERE id=1"; } else { $sql = "UPDATE game_character SET current_hp = (current_hp-100) WHERE id=1"; } $statement = $dbh->prepare($sql); $statement->execute(); Quote Link to comment https://forums.phpfreaks.com/topic/245915-not-getting-any-errors-but-its-not-updating-my-database/#findComment-1262966 Share on other sites More sharing options...
cloudll Posted August 28, 2011 Author Share Posted August 28, 2011 Yep both queries have been tested and working, untill I put them in the if else. Im not too sure what you mean about the first part of my conditional won't be executed. I cant see why. Quote Link to comment https://forums.phpfreaks.com/topic/245915-not-getting-any-errors-but-its-not-updating-my-database/#findComment-1262968 Share on other sites More sharing options...
Jacbey Posted August 28, 2011 Share Posted August 28, 2011 Here, try this... You need to actually give a mysql_query() so this would be: <?php if ($enemy_hp <= 0) { $sql = "UPDATE game_status SET battle = '1' WHERE id=1"; mysql_query($sql); } else { $sql = "UPDATE game_character SET current_hp = (current_hp-100) WHERE id=1"; mysql_query($sql); $statement = $dbh->prepare($sql); $statement->execute(); } ?> The above should work, good luck. Quote Link to comment https://forums.phpfreaks.com/topic/245915-not-getting-any-errors-but-its-not-updating-my-database/#findComment-1262970 Share on other sites More sharing options...
Pikachu2000 Posted August 28, 2011 Share Posted August 28, 2011 Im not too sure what you mean about the first part of my conditional won't be executed. I cant see why. Look at the logic in your post. The if() defines a query. The else defines and executes a query. There is nothing to execute the query in the if() the way it's written. In the code I posted, the logic has been changed so that a query is defined in the if() and in the else, then whichever query is defined is actually executed after the conditional. Quote Link to comment https://forums.phpfreaks.com/topic/245915-not-getting-any-errors-but-its-not-updating-my-database/#findComment-1262973 Share on other sites More sharing options...
cloudll Posted August 28, 2011 Author Share Posted August 28, 2011 Ah ok I get it now, cheers. Thanks for the examples guys but im still having problems. I tried the way you posted Pikachu2000, It works more than my way did. But it executes this part when enemy hp is well above 0. if ($enemy_hp <= 0) { $sql = "UPDATE game_status SET battle = '1' WHERE id=1"; Quote Link to comment https://forums.phpfreaks.com/topic/245915-not-getting-any-errors-but-its-not-updating-my-database/#findComment-1262977 Share on other sites More sharing options...
Pikachu2000 Posted August 28, 2011 Share Posted August 28, 2011 Are you sure $enemy_hp has a value at that point? Echo (or preferably var_dump() ) it and see what it actually is. Quote Link to comment https://forums.phpfreaks.com/topic/245915-not-getting-any-errors-but-its-not-updating-my-database/#findComment-1262978 Share on other sites More sharing options...
cloudll Posted August 28, 2011 Author Share Posted August 28, 2011 I have it echo'd, seems to be working fine. Quote Link to comment https://forums.phpfreaks.com/topic/245915-not-getting-any-errors-but-its-not-updating-my-database/#findComment-1262979 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.