Jump to content

Not getting any errors but its not updating my database.


cloudll

Recommended Posts

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();

}

?>

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.