skip to Main Content

I wrote some code that enters data into my sql database using JQuery and PHP and it works.
However, I need the error block of the Ajax request to be executed when the database server is offline, sql throws an error, or whenever there should be an error.

The problem is, that the error-block of the ajax request never is executed. Always just the success block. No matter if the sql query is wrong or the database server is offline.

I have tried it with a fail-block and with jQuery.$.get() but that doesn’t work either. But I prefer an ajax request anyway.

I have written the following code so far:

//JavaScript-function to insert data into a database. The parameter is an SQL-INSERT statement.
function insertIntoDatabase(sqlQuery)
{
    var result;

    $.ajax({
        type: "POST",
        url: "../../general/clientHelper.php",
        data: {sql: sqlQuery},
        async: false,
        error: function()
        {
            if(sqlQuery.split(" ")[0] != "INSERT") console.log("SQL-Query is not an INSERT statement");
            result = false;
        },
        success: function()
        {
            result = true;
        }
    });
    
    return result;
}
<?php

//clientHelper.php - Insert data into the database.

if(isset($_POST['sql'])) insertIntoDatabase($_POST['sql']);
function insertIntoDatabase($sqlQuery)
{
    $ip = "10.10.10.1";
    $port = 3306;
    $username = "candidate";
    $password = "candidate";
    $dbname = "cqtsdb";

    $connection = new mysqli($ip, $username, $password, $dbname, $port);
    $connection->query($sqlQuery);
    $connection->close();
    
    exit();
}
?>

I don’t know what to do now :/ Please help <3

2

Answers


  1. Chosen as BEST ANSWER

    UPDATE:

    I found out that if I add one parameter to the success function it gets filled with the text of an error if one has occurred. If everything is right the text is just "". So I didn't have to do anything else than check for it :)

    success: function(data)
    {
        if(data == "") result = true; 
        else  result = false;
    }
    

  2. Check the query result, if it was successful then return a certain value to ajax like 1, if it wasn’t, return 0.
    Then in ajax success function check that value and show a message accordingly or whatever you want to do.

    I use procedural PHP and I do it this way

    function leave($msg, $conn, $type){
        //$msg is the message I want to display to the user.
        //$type is the query result type I explained above.
        //$conn is to close the connection.
        echo json_encode(array(
            "msg" => $msg,
            "type" => $type
        ));
        mysqli_close($conn);
        exit();
    }
    

    call this function that way

    $result = mysqli_query($conn, $query);
        if ($result) {
            leave('done', $conn, 1);
        } else {
            leave('something went wrong', $conn, 0);
        }
    

    check the value like that:

    ...
    success: function (response) {
          if(response.type == 1){
              // do smth
          }
          else if(repsonse.type == 0){
              // do another thing
          }
        },
    ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search