vendredi 25 juin 2021

PHP if else statement seems not working correctly

I have the following code, which is not working as expected

global $con;   // Mysqli Object
echo "ID is: ".$id;

if($id != 0){
    echo "ID is: ".$id;
    // Update
    $con->query("update table set type = '$type',
                                    from = '$from',
                                    comments = '$comments'"
        . "where id = '$id'");
}else{
    echo "ID is: ".$id;
    echo "This is not executed";
    // New
    $con->query("insert into table(type, from, comments)
        values ('$typ', '$from', '$comments')");
    echo "This is not executed";
}

If i run this code with ID = 26 i get the following results:

Text output (as expected): ID is: 26ID is: 26

Result in Database: Entry with ID 26 is updated correctly AND there are 2 new entries with empty values. If i remove the insert into lines or comment them i see only the correct update in database, no 2 new entries. It seems that the insert statement is executed but the echoes before and after or any other method in else block is not. Any ideas what is wrong here?

Aucun commentaire:

Enregistrer un commentaire