I'm new to php and I get stuck to find the max(id) by given a specific id.
In activity A, there has a listView. When the list detect long press, it will check whether the id is the max id. If yes, it will deleted, if not, it will display list cannot be deleted.
This is what I have tried.
listViewUpdate.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(final AdapterView<?> p, View v, final int po, long id) {
iD = details1.get(po).getID();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Delete");
builder.setMessage("Are you sure you want to delete?");
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int ii) {
checkMaxID(ID,iD); // ID is foreign key
// delete(iD);
objadapter.removeItem(po);
}
});
public void checkMaxID(final int foreignKey,final String iD)
{
class check extends AsyncTask<Void,Void,String>{
// ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
// loading = ProgressDialog.show(Edit_Staff.this,"Updating...","Wait...",false,false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
// loading.dismiss();
Toast.makeText(getActivity(), s, Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(Void... params) {
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put(Configs.KEY_ID, iD);
hashMap.put(Configs.KEY_TWD, String.valueOf(foreignKey));
RequestHandler rh = new RequestHandler();
String s = rh.sendPostRequest(Configs.URL_CHECK_ID, hashMap);
return s;
}
}
check ue = new check();
ue.execute();
}
checkID.php
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
//Getting values
$id = $_GET['id'];
$foreignKey = $_GET['twd'];
//Creating an sql query
$sql = "SELECT MAX(id) FROM work_details WHERE twd = '$foreignKey'";
$row = mysqli_fetch_row($sql);
$maxId=$row[0];
//Importing our db connection script
require_once('dbConnect.php');
//Executing query to database
if($id==$maxId){
$sql = "DELETE FROM work_details WHERE id=$id;";
}else{
echo 'list cannot be deleted';
}
//Closing the database
mysqli_close($con);
}
?>
When I click yes in the confirmation dialog, I get error.
Error
Can someone help ? Thanks
