1

i have problem here about inserting data using ajax with JQGrid, when i try pass the data its say succses but not stored in database , i think i have problem with ajax or add function in php here the ss

here my ajax

function AddPost(params) {

    $.ajax({
        url: '<?php echo base_url() ?>index.php/Welcome/tambah_data',
        data: {
            nama: params.nama,
            deskripsi: params.deskripsi,
            user_id: params.user_id,
            created_time: params.created_time,
        },
        async: 'true',
        cache: 'false',
        type: 'post',

        success: function (data) {
            alert("Data successfully added");
        },
        error:function(data){
            alert("error happend");
        }
    }); 

    console.log(params);            

}

and here my controller

    function tambah_data(){

$id=$_POST['id'];
$name=$_POST['name'];
$deskripsi=$_POST['deskripsi'];
$user_id=$_POST['user_id'];
$created_time=$_POST['created_time'];

$sql = "INSERT INTO `master_matakuliah`( `id`, `name`, `deskripsi`, `user_id`,`created_time`) 
VALUES ('$id','$name','$deskripsi','$user_id','$created_time')";
if (mysqli_query($conn, $sql)) {
    echo json_encode(array("statusCode"=>200));
} 
else {
    echo json_encode(array("statusCode"=>201));
}
mysqli_close($conn);

}

can some help me with this ? , sorry for my bad english,thanks

Lulut
  • 109
  • 9
  • 1
    You have not passed `$_POST['id']` this in your ajax call , also there is typo in ajax call `nama` you have pass from ajax to php but getting it using `$_POST['name']` this will be `null`. – Swati Mar 19 '20 at 06:13
  • thanks for reply @Swati , im already add id on ajax , and change name to nama , buts stil doesnt work , am i wrong in ajax or php ? – Lulut Mar 19 '20 at 07:37
  • I think the problem is in php , try to print [mysqli_error](https://stackoverflow.com/questions/12227626/how-do-i-display-a-mysql-error-in-php-for-a-long-query-that-depends-on-the-user) this will show error if there is any in your query. – Swati Mar 19 '20 at 08:22
  • The very easy way to check the problem is to return the $sql variable in your ***echo json_encode***. After this try to manually execute this command in console and you will see the problem – Tony Tomov Mar 19 '20 at 10:47

0 Answers0