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:
.htaccess
like hereSOLVED! 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.