Overwriting the same row with ajax

Max

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.

Martin Heralecký

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.

edited at
0

Comments

0 comments
Login to comment

Related

Concatenate a list of hashmap into one on the same otder they are and without overwriting the key

Output to the same line overwriting previous output?

Requiring a LuaJIT module in a subdir is overwriting a module of the same name in the parent dir

Overwriting row in same csv file using dictwriter

Logic Apps Blob overwriting file of same name

Excel VBA Copy/Paste overwriting same row

VBA: Next Empty Row Randomly Overwriting Some Rows

How to change the value of a specific row of table by clicking on another value on the same row using ajax

How to stop overwriting on the same file in flutter?

deleting and creating new/overwriting file with same name c# .NET

NuGet - disallow overwriting packages (with same name and version number)

Keep old returned Ajax data in div instead of overwriting

Sparse BLAS matrix-row vector product overwriting loop index

SSD wear for overwriting the same bytes

Create files with same name without overwriting java

rails ajax overwriting all partials when refreshing page

VBA - Skipping rows and overwriting the row

Download attachments with same name without overwriting in Java

Displaying the details of a row clicked and details fetched from database in same jsp using ajax

How to auto change select in accordance with same row table using jQuery ajax change function

Overwriting file of the same name from a trimmed file

Pandas try to append a row to a dataframe but keeps overwriting the existing row

Load ReactJS component on the same page overwriting other components

Need to overwrite cells in a column without overwriting the other cells in the same column

Moq Setup Adding With Same "Filter" instead of overwriting

Ruby CSV - Write on same row without overwriting?

Array row is overwriting another rows

Skip first row when overwriting the CSV file in PHP

Pandas to_csv is overwriting first row with header=