-1

I have a SQL string that updates my DB

I have left out most of the insert code as this is not where I need help. I run the string updating numerous tables but I need to stop the string once an error is detected.

I have in the first SQL string a Unique coulomb that i do not want duplicates of but in the other tables duplicates of the same coulomb doesn't matter. As this is history tables keeping history of the objects

This section is the sql section thereafter I start the sql 1

if ($conn->query($sql) === TRUE) {
    echo "Client and Unit has been loaded  ";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
     echo("Error description: " . mysqli_error($con));
      echo "Error in uploading information";
}
    $sql1 = "INSERT INTO unit_history SET 

The error is correct and the table is not populated but it goes on to the next sql1 string I need it to stop once the error is found.

Duplicate entry '111' for key 'unitnr' Notice: Undefined variable: con in client_rep.php on line 120

Is there a way I can Kill the script once error is noted on duplicate entry?

Trevor Ackermann
  • 166
  • 1
  • 4
  • 16
  • 1
    Possible duplicate of [What are the differences in die() and exit() in PHP?](https://stackoverflow.com/questions/1795025/what-are-the-differences-in-die-and-exit-in-php) – glennsl Oct 09 '17 at 12:35
  • Use 1 of these `return;`, `die;` or `exit;` – mega6382 Oct 09 '17 at 12:37
  • If all the data is linked, you may want to look into transactions as well so you either get all of the updates or nothing. – Nigel Ren Oct 09 '17 at 12:41

1 Answers1

1

If you want to stop execution only then use die(); or exit();

Or

You can return from there:

if ($conn->query($sql) === TRUE) {
    echo "Client and Unit has been loaded  ";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
     echo("Error description: " . mysqli_error($con));
      echo "Error in uploading information";
     die; // Use die or exit here.
}
Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33