javascript发布到php for mysql insert record

I have read many tutorials on this but just can't seem to get this to work - I have the following PHP file on the server:

<?php
    $host_name  = "";
    $database   = "";
    $user_name  = "";
    $password   = "";

    $connect = mysqli_connect($host_name, $user_name, $password, $database);

    $user = $_POST['user'];
    $sports = $_POST['sports'];
    $location = $_POST['location'];

    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $sql = "UPDATE `userActivityLocation` WHERE user='$user' SET sports='$sports', location='$location'";
    $result=mysql_query($sql);
?>

I have tried several methods to no avail, not sure what Im doing wrong! Any advice would be greatly appreciated, thank you!

EDIT: I have tried the following as suggested by morgunder but I still cant see any record being created in the database.

Here is the click listener for the submit button:

$('#register').click(function () {
        console.log("Ready! Getting the user details.");

        username = document.getElementById('login').value;
        password = document.getElementById('password').value;
        email = document.getElementById('email').value;
        window.localStorage.setItem("user", username);
        console.log("Username: ", username);
        console.log("Password: ", password);
        console.log("E-Mail: ", email);

        var params, connection;

        params = {
            login: username,
            password: password,
            email: email
        };

        //console.log("Params: ", params);
        console.log("Creating new user");
        QB.users.create(params, function (err, result) {
            console.log("Result: ", result.toString());
            // callback function
            alert(username.toString());
            var usernamePost = username.toString();
            $.post("http://www.baseurl.co.uk/API/users.php", { user: usernamePost }, function (data) {
                console.log(data);
                console.log("HERE");
            });

            window.location = "sportsSelector.html"
        });
    });

You've said, that you want to insert record, but you use UPDATE SQL command. That's why the record isn't being created.

Try this:

$sql = "INSERT INTO `userActivityLocation`(user,sports,location) VALUES ('$user', '$sports', '$location')";

Also there are some thing to consider:

  • jQuery is definitely easier, than plain old xhr object. And it handles cross-browser issues for you!
  • If the API is on another server than the client you should set up some kind of cross origin filter on the server site (You can do it in .htaccess like here
  • "Gluing" SQL from strings is not safe because of possible SQL injection. Prepared statements are the answer to that problem

SOLVED! With help from you lovely people, but the final answer was an entirely separate less obvious issue. The event was firing, the post was starting, the server side code was good, the javascript was good after being fixed, but then it was being rudely interrupted by the window.location call! This is what was breaking the transaction.