How to get useful error messages in mysql query in php? [duplicate]

In the else-block you can see, how you can catch up MySQL-errors and display them in an alert.

if(isset($_POST['btnSubmit'])) {
    $name = mysqli_real_escape_string($conn,$_POST["name"]);
    $sql = "INSERT INTO isodetail(title) VALUES ('{$name}')";
    $run = mysqli_query($conn, $sql);

    if($run) {
        echo "<script>alert('Certi Added Successfully');window.open('isocerti.php','_self');</script>";
    } else {
        $error = addslashes(mysqli_error($conn));
        echo "<script>alert('An Error occur: {$error}');</script>";
    }
}

References:
MySQLi-Error: http://php.net/manual/en/mysqli.error.php
Escape special characters: http://php.net/manual/en/function.addslashes.php (it does the job for this example)

Leave a Comment