1

I've been trying to post data using AJAX that will update a field in my database however I am having trouble doing so. Everything seems like it should run fine and I get no errors in the console but I've no idea why my db won't update.

Can someone help me out here please?

AJAX:

function ajaxUpdate() {
        var arr = {var1: name, var2: age};
            $.ajax({
                url: 'ajax/confirm.php',
                type: 'POST',
                data: JSON.stringify(arr),
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function(data) {
                    console.log("success");
                }
            });
        }

Confirm.php:

$name=$_POST['var1'];
$age=$_POST['var2'];

if($name == "Stuart") {
    mysqli_query($connection,"UPDATE people SET age='$age'");
}
else if($name == "Peter") {
    mysqli_query($connection,"UPDATE people SET age='$age'");
}

The connection to my database is working as I have $connection setup and went to the page /ajax/confirm.php in my browser and I see "Connection successful" in my console as I defined if successful.

So I am unsure as to why this isn't updating?

Are my values not being posted correctly?

I'm new to AJAX so forgive me if this is something very simple!

Thanks

11
  • data: {var1: name, var2: age} from where name and age data will come? Commented Sep 29, 2017 at 11:33
  • Where's the WHERE condition in your update ? Commented Sep 29, 2017 at 11:33
  • 1
    And if name is different then Stuart or Peter nothing will happen ? Commented Sep 29, 2017 at 11:34
  • Sorry I just updated my post to reflect the ajax I'm using. Commented Sep 29, 2017 at 11:35
  • 1
    You are not sending key-value pairs to the server but one json encoded string. You need to get and parse that on the server or send key-value pairs instead. And sql injection. Commented Sep 29, 2017 at 11:37

2 Answers 2

1

Try the following:

function ajaxUpdate() {
    var arr = {var1: name, var2: age};
        $.ajax({
            url: 'ajax/confirm.php',
            type: 'POST',
            data: arr,
            success: function(data) {
                console.log("success");
            }
        });
}

Instead of converting the object into json string send it as is.

Edit: Also remove dataType and probably contentType too. Your code is at risk of SQL Injection. Look into prepared statements and escaping mysql data.

Sign up to request clarification or add additional context in comments.

1 Comment

you legend, worked! I've been plucking my brains for the last 2 hours with this too! Thank you!
0

Maybe this well help.

<script type="text/javascript">

    function ajaxUpdate() {
        var data = $('#formID').serialize();
            $.ajax({
                url: 'ajax/confirm.php',
                type: 'POST',
                data: data,
                dataType: 'json',
                encode : true,
                success: function(data) {

                    if(data == "ok"){
                        console.log("success");
                    }else{

                        console.log(data);
                    }
                }
            });
        }
</script>

confirm.php

<?php

$name = $_POST['name'];
$age  = $_POST['age'];


switch ($name) {
    case 'Stuart':
        $sql  = "UPDATE people SET age = ? WHERE name = ? ";
        $stmt = mysqli_prepare($connection, $sql);
        mysqli_stmt_bind_param($stmt, 'si', $name, $age);
        if (mysqli_stmt_execute($stmt)) {
            echo json_encode('ok');
        } else {

            echo json_encode(mysqli_stmt_error($stmt));
        }
        break;
    case 'Peter':
        $sql  = "UPDATE people SET age = ? WHERE name = ? ";
        $stmt = mysqli_prepare($connection, $sql);
        mysqli_stmt_bind_param($stmt, 'si', $name, $age);
        if (mysqli_stmt_execute($stmt)) {
            echo json_encode('ok');
        } else {

            echo json_encode(mysqli_stmt_error($stmt));
        }
        break;

    default:

        echo json_encode('Unknown name ');
}

1 Comment

Why would this help? A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.