0

This is my php file : I want to Insert data into detaildonasi based on last id in donasi table

<?php

$conn = new mysqli("localhost","root","","donasiku");
$sql = "SELECT * FROM donasi ORDER BY id DESC LIMIT = 1";
$result = $conn->query($sql);

if($result === TRUE)
{
    $id = $result->fetch_assoc();
    $jumlah = $_POST['jumlah'];
    $kebutuhanid = $_POST['kebutuhanid'];

    $sql2 = "INSERT INTO detaildonasi VALUES('','$jumlah','$id','$kebutuhanid')"
    $result2 = $conn->query($sql2);

    if($result2 === TRUE)
    {
        $arr = array('hasil' => 'success');
    }
    else
    {
        $arr = array('hasil' => $conn->error);
    }
}
else
{
    $arr = array('hasil' => $conn->error);
}

echo json_encode($arr);
$conn->close();

?>

This is the Volley of my Fragment file :

val q2 = Volley.newRequestQueue(this.context)
val url2 = "http://10.0.2.2/donasiku/detaildonasi.php"
val sr2 = object:StringRequest(Request.Method.POST,url2,Response.Listener {
      response -> try{
      val obj2 = JSONObject(response)
      Toast.makeText(this.context,obj2.getString("hasil"), Toast.LENGTH_SHORT).show()
      }catch (e:JSONException){Toast.makeText(this.context, e.message.toString(), Toast.LENGTH_SHORT).show()}
      },Response.ErrorListener {  })
      {
        override fun getParams(): Map<String, String> {
        val params2 = HashMap<String, String>()
        for(donasi in daftarDonasiUser)
         {
            params2.put("jumlah",donasi.jumlahbarang.toString())
            params2.put("kebutuhanid",donasi.idbarang.toString())
         }
            return params2

            }
         }

         q2.add(sr2)

The code goes into catch JSONException and Toast Value br of type java.lang.String cannot be converted to JSONObject.

Catharina
  • 29
  • 5

1 Answers1

0

<br is html. Your api is erroring in some way and so html of some kind (instead of the Json you are expecting) is being written to the page.Try making the same request in Postman and viewing the response.

Ivan Wooll
  • 4,145
  • 3
  • 23
  • 34