am struggling with what seems should be a simple, thing. Namely to update database entry for a user with a variable. Here is the code am using:
function UpdateDB(username){
alert(username);
//username="John";
$.ajax({
async: false,
dataType: 'json',
type: "POST",
url: "file.php",
data: {'username':username},
});
}
function is called onlick, and username is defined as :var username=document.getElementById("username").innerHTML;. Both are defined before function is called and should not cause any problems. Alert gives correct var value. Associated bit in external PHP file (file.php) is:
if(isset($_POST['username'])){
$username=$_POST['username'];
$query = "UPDATE users SET Country='USA' WHERE username='$username'";
if(mysqli_query($db, $query)){
echo "Records were updated successfully.";
} else {
echo "ERROR: Could not able to execute $query. " . mysqli_error($db);
}
}
However, Country entry does not get updated for a var username in the database. However it works fine if instead of using variable, i just uncomment //username="John";. What am i doing wrong? Thank you.