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.

Matt S
  • 14,976
  • 6
  • 57
  • 76
kimdirbilmem
  • 61
  • 1
  • 7

1 Answers1

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.

Chris
  • 5,571
  • 2
  • 20
  • 32
  • Normally, no you don't want to nest them. The best option is to tell mysqli to [throw exceptions](http://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) and then have a single `catch` block that handles all unexpected failures. – Chris Apr 24 '16 at 20:13