HOW TO UPDATE DATA IN THE DATABASE USING PHP WITH AJAX

Hey guys, Today we are learning MYSQLI UPDATE QUERY execute in the database Using PHP with AJAX.

1. HTML Source Code(Index.php)

<?php
$conn=mysqli_connect("localhost", "root", "", "testproject");
$fetchQry=mysqli_query($conn, "SELECT * FROM userdetails WHERE id=1");
$rows=mysqli_fetch_array($fetchQry);
?>

<form action="#" method="post">
<input type="hidden" name="id" value="<?php echo $rows['id']; ?>">
<input type="text" name="name" palceholder="Enter name" value="<?php echo $rows['name']; ?>">
<input type="submit" name="submit" value="Submit">
</form>

<script>
$(function () {
$('form').bind('submit', function () {
$.ajax({
type: 'post',
url: 'send.php',
data: new FormData($('form')[0]),
cache: false,
contentType: false,
processData: false,
success: function (data) {
if(data=='success'){
alert("Data has been successfully updated");
}
else{
alert("datahas been updated failed");
}
}
});
return false;
});
});
</script>
2. PHP Source Code(send.php)

<?php
$conn=mysqli_connect("localhost", "root", "", "testproject");
$name=$_POST["name"];
$id=$_POST["id"];
$query=mysqli_query($conn, "UPDATE userdetails SET name='$name' WHERE id='$id'");
if(query==true){
echo"success";
}
else{
echo"Updated Failed";
}
?>