0

I'm trying to update a row using Ajax, it failed.

Here my code

<button onclick="updateAjax(<?php echo $row['id_doc']; ?>,this)" class="btn btn-danger">Delete</button>

function updateAjax(id_doc,obj){
$.ajax({

  type: 'get',
  url: 'toIndex.php',
  data: {nama_file:id_doc},

  success:function(data){
    data = JSON.parse(data);

    if(data){

      alert("Data berhasil diupdate");

    }else{

        alert("Data gagal diupdate");
    }
  }

}); }

On the toIndex.php page

$nama_file = $_GET['nama_file'];
$update = "UPDATE documents SET status_index = 1 WHERE nama_file='".$nama_file."'";
if($conn->query($update) == TRUE){
   echo json_encode(true);
}else{
   echo json_encode(false);
}

the alert("Data berhasil diupdate") is work, but the mysqli query is failed to update the data. Any solution?

Bits Please
  • 877
  • 6
  • 23
Juli Andika
  • 57
  • 10
  • 3
    Ajax function must be in jquery script. It seems you have written in php. – Geee Apr 04 '18 at 04:57
  • `Add dataType = 'JSON'` if you want responce as JSON format – Geee Apr 04 '18 at 05:00
  • put your query code in a `try{..}catch{...}` block see what error you get – Ikhlak S. Apr 04 '18 at 05:02
  • Make sure $row['id_doc'] has a value and if non numeric its php tags should be enclose in `''` – Karlo Kokkak Apr 04 '18 at 05:07
  • enclose you js script in – Karlo Kokkak Apr 04 '18 at 05:13
  • 1
    Your script is at risk of [SQL Injection Attack](https://stackoverflow.com/q/60174/5914775). Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/). Even [if you are escaping inputs, its not safe!](https://stackoverflow.com/q/5741187/5914775). Use [prepared parameterized statements](https://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead. – Tom Udding Apr 04 '18 at 05:14

1 Answers1

0

send data like this

 data: 'nama_file='+nama_file+'&id_doc='+id_doc,