Can’t Input Data to MySQL Using Fast Android Network From Android Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Can’t Input Data to MySQL Using Fast Android Network From Android without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I try to make simple CRUD with Android and MYSQL , PHP for server REST API. I use Fast Android Network Library, and everything is ok and runing, but if i click “Tambah Data / Add Data” it SHow ” Failed add to database ” . I have some code here for function Post Data.

public void tambahData(String nim, String nama, String kelas){
    //koneksi ke file create.php, jika menggunakan localhost gunakan ip sesuai dengan ip kamu
    AndroidNetworking.post("http://192.168.1.5/mahasiswa/create.php")
            .addBodyParameter("id_mahasiswa", "") //id bersifat Auto_Increment tidak perlu diisi/(diisi NULL) cek create.php
            .addBodyParameter("nim",nim) //mengirimkan data nim_mahasiswa yang akan diisi dengan varibel nim
            .addBodyParameter("nama", nama) //mengirimkan data nama_mahasiswa yang akan diisi dengan varibel nama
            .addBodyParameter("kelas", kelas) //mengirimkan data kelas_mahasiswa yang akan diisi dengan varibel kelas
            .setPriority(Priority.MEDIUM)
            .build()
            .getAsJSONObject(new JSONObjectRequestListener() {
                @Override
                public void onResponse(JSONObject response) {
                    //Handle Response
                    Log.d(TAG, "onResponse: " + response); //untuk log pada onresponse
                    Toast.makeText(getApplicationContext(),"Data berhasil ditambahkan" , Toast.LENGTH_SHORT).show();
                    //memunculkan Toast saat data berhasil ditambahkan

                }
                @Override
                public void onError(ANError error) {
                    //Handle Error
                    Log.d(TAG, "onError: Failed" + error); //untuk log pada onerror
                    Toast.makeText(getApplicationContext(),"Failed add to database", Toast.LENGTH_SHORT).show();
                    //memunculkan Toast saat data gagal ditambahkan
                }
            });
}

}

Here create.php i have :

    <?php
if($_SERVER['REQUEST_METHOD']=='POST'){

//include file connect.php untuk menyambungkan file create.php dengan database
include "config.php";

//inisialisasi variabel yang akan ditampung dan diolah dengan query
$id = $_POST['id_mahasiswa'];
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$kelas = $_POST['kelas'];

//inisialiasi query INSERT  
$query = "INSERT INTO tb_mahasiswa(id_mahasiswa,nim,nama,kelas) VALUES ('','$nim','$nama','$kelas')";

//pemanggilan fungsi mysqli_query untuk mengirimkan perintah sesuai parameter yang diisi
$sql = mysqli_query($connect, $query);

//pengkondisian saat fungsi mysqli_query berhasil atau gagal dieksekusi
if($sql){
     $response["value"] = 1;
     $response["message"] = "Sukses tambah data";
     echo json_encode($response); //merubah respone menjadi JsonObject
}else{
     $response["value"] = 0;
     $response["message"] = "Gagal tambah data";
     echo json_encode($response); //merubah respone menjadi JsonObject
}
//tutup koneksi
mysqli_close($connect);
}
?>

what wrong ? and Can Anyone Solve that ? Thanks

Answer

You missing to pass the ID value to query. In your table, if you have ID column is not null filed and it is not auto-generated means. Query won’t execute. If you post your database table structure might be good to understand well.

We are here to answer your question about Can’t Input Data to MySQL Using Fast Android Network From Android - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji