Do not run any query if one of the queries has an error. How do i do that?

166
January 04, 2020, at 09:30 AM

I noticed something today and thought that it could cause huge problems in the future. Let me explain;

Example; I have a like this

$query1 = "INSERT INTO table1(col1,col2,col3) VALUES('$col1','$col2','$col3')";
$query2 = "INSERT INTO table2(col1,col2,col3) VALUES('$col1','$col2','$col3')";
$query3 = "UPDATE table3 SET col1 = '$col1' WHERE id='$id'";
if (mysql_query($query1) && mysql_query($query2) && mysql_query($query3)) {
    echo "successful message";
}
else {
    echo mysql_error();
}

When this happens, non-error queries are processed. So if one of them fails, the statistics screen is wrong.

What I want to do;

If there is an error in one of the queries, none should be processed. How can i do that?

(Im sorry for my english.)

Answer 1

What you want to achieve can't be done using the mysql_ driver (another reason to update). With PDO or mysqli you can use a transaction to achieve this behavior. You also should take advantage of their prepared statements as well.

  1. https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php
  2. https://www.php.net/manual/en/pdo.prepared-statements.php

Then once you have that done, with whichever driver you choose (PDO is preferable, IMO), See:

  1. https://www.php.net/manual/en/mysqli.begin-transaction.php
  2. https://www.php.net/manual/en/pdo.begintransaction.php
Rent Charter Buses Company
READ ALSO
Generate List of Employees Weekly and Monthly [closed]

Generate List of Employees Weekly and Monthly [closed]

Want to improve this question? Add details and clarify the problem by editing this post

169
addEventListener scroll Uncaught TypeError: element.getBoundingClientRect is not a function

addEventListener scroll Uncaught TypeError: element.getBoundingClientRect is not a function

I'm trying to set up my code so it can detect if a certain section of my HTML is at the top of the viewportI'm getting the error:

208