Can't execute sql request via ajax request, use the same type of ajax request and work fine everywhere except this

I use this ajax call everywhere and it works like a charm, but only this gives the problem. I do not understand what the problem is? Why am I getting "Re-write error": "I still don't see any problems and it's so easy.

PS: I echoed the values ​​on the ajax side response and got all three values, but the database is giving the problem. Moreover, the same sql query works fine on the database side, but when it deals with ajax and php it gives a problem.

Values ​​went fine: id1 = Get-version & id2 = machine055 & id3 = VMLAB

DB Query looks ok: SELECT version

FROM table1

where machine

= 'machine055'

Can anyone help me find a solution for this?

enter code here

PHP+UI:

serverChange.on('change', function(){
            $("#progress").show();
            var selectedServer = $(this).val();

            $.ajax({
                type: "POST",
                url: "/web/scripts.php",
                data: { id1: "get-version", id2: selectedServer, id3: "vmlab" },
                success:function(msg){
                    $("#progress").hide();
                    alert(msg);
                }
            });

        });


PHP+Ajax:

if ($_POST['id1'] == "get-version" && $_POST['id3'] == "vmlab") {

    $conn = mysqli_connect("localhost", "root", "pwd", "db");
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT `version` FROM `table1` where `machine` = '" .trim($_POST['id2']). "'";
    if ($conn->query($sql) === TRUE) {
        echo "Machine retrieved successfully.";
    } else {
        echo "Error retried record: " . $conn->error;
    }

      

+3


source to share


1 answer


I tried a different approach and it works great, so I don't understand much, here is the solution:

    $conn = mysqli_connect("localhost", "root", "pwd", "db");
     if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT `version` FROM `table1` where `machine` = '" .trim($_POST['id2']). "'";
    $result = mysqli_query($conn, $sql);

    while($row =  mysqli_fetch_array($result)) {
        $rows[] = $row['version'];
    }

    echo ($rows[0]);

    $conn->close();

      



Thanks everyone for reading the question and resolution.

+1


source







All Articles