I'm new to php
, now trying to send arrayList
data from android to Php MySQL
.
In table work_details
, I have 7 column (id, project, work_description, percentage, time_in, time_out, fk)
. Now I want to save the arraylist
and fk
to the table.
I have tried to code but I know this is not the correct way.
public void addWorkDetails(ArrayList<SearchResults> listItems, final long id) // listItems have project,workDescription,percentage,timeIn,timeOut
{
final JSONObject object= new JSONObject();
for(int i=0;i<listItems.size();i++)
{
try
{
object.put("Count : "+String.valueOf(i + 1),listItems.get(i));
}catch(JSONException e)
{
e.printStackTrace();
}
}
class AddWorkDetails extends AsyncTask<String, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(WorkDetailsTable.this, "Please Wait",null, true, true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<String,String>();
data.put("listItems",String.valueOf(object)); // not sure
data.put(Config.KEY_TWF,String.valueOf(id));
RequestHandler rh=new RequestHandler();
String result = rh.sendPostRequest(Config.ADD_WORKDETAILS,data);
return result;
}
}
AddWorkDetails ru = new AddWorkDetails();
ru.execute("listItems",String.valueOf(id)); // not sure
}
Php
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$list[]=$_POST['listItems'];
$id=$_POST['id'];
foreach($list as $value){
$value=mysqli_real_escape_string($val);
$sql="INSERT INTO work_details (project, work_description, percentage, timeIn, timeOut, id) VALUES ('$val', '$id')";
//Importing our db connection script
require_once('dbConnect.php');
//Executing query to database
if(mysqli_query($con,$sql)){
echo ' Added Successfully';
}else{
echo 'Could Not Add Data';
}
//Closing the database
mysqli_close($con);
}
}
?>
Error
Any help would be greatly appreciated !!!