0

I am currently working on a website project. And for now in my sign.php file, at least 5 sql statement must be done. Normally I use mysqli_query function and if it does not return false, pass the next one. But what if one of them returns false? Previous statements are okey but remaining ones are going to be failed. So the user can not completely sign to system, only half of the database is updated.

if( mysqli_query( $con, $sql ) )
{
    //do the next one
}
else
{
     //everything messed up 
}

This way does not confort me. The algorithm is poor quality. I do not know how to handle this problem. What is the best path I can follow to implement these operations?

Note: All operations are different. I have to use SELECT, INSERT, CREATE, UPDATE... Note2: If something is missed, I can edit the question.

1 Answer 1

2

You can use SQL transactions to ensure that if any of your queries fail changes to the database made by previous queries are rolled back.

Sign up to request clarification or add additional context in comments.

1 Comment

Normally, no you don't want to nest them. The best option is to tell mysqli to throw exceptions and then have a single catch block that handles all unexpected failures.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.