I have the following ajax and php files to enter data to my current mytable table. The below codes work together perfectly fine as they are.
Ajax file:
<script type="text/javascript">
function insertData() {
var stata=$("#stata").val();
var statb=$("#statb").val();
var statc=$("#statc").val();
$.ajax({
type: "POST",
url: "update.php",
data: {stata:stata,statb:statb,statc:statc},
dataType: "JSON",
success: function(data) {
$("#message").html(data);
$("p").addClass("alert alert-success");
},
error: function(err) {
alert(err);
}
});
}
</script>
update.php
<?php
include('db.php');
$stata=$_POST['stata'];
$statb=$_POST['statb'];
$statc=$_POST['statc'];
$stmt = $DBcon->prepare("REPLACE INTO mytable (stata,statb,statc) VALUES(:stata, :statb,:statc)");
$stmt->bindparam(':stata', $stata);
$stmt->bindparam(':statb', $statb);
$stmt->bindparam(':statc', $statc);
if($stmt->execute())
{
$res="Data Inserted Successfully:";
echo json_encode($res);
}
else {
$error="Not Inserted,Some Problem occur.";
echo json_encode($error);
}
?>
What I'd like to do is to overwrite the same row (update or replace)in the table ( like where id=1) every time I enter the data.
But when I add "Where id=1"
to the sql query, I get [object Object] error on the screen. I couldn't make it work any way I tried. How can I overcome this problem? thanks.
If you want to update the row (not replace it), use UPDATE
instead of REPLACE
.
UPDATE mytable SET stata = :stata, statb = :statb, statc = :statc WHERE id = 1
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments