-1

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

enter image description here

Can someone help ? Thanks

6
  • Is that error message a custom written one? Give more details, because we cannot help you with such scarce error information. Commented Jan 25, 2016 at 18:15
  • @MikeM. I'm sorry, I didn't get answer from my previous post..Please help me..Thanks Commented Jan 25, 2016 at 18:19
  • Anyone please??? I'm stuck from Morning until Now ! Commented Jan 25, 2016 at 18:30
  • @Milkncookiez I edited my post Commented Jan 25, 2016 at 18:55
  • 1
    @John, I answered. :) Commented Jan 25, 2016 at 23:23

2 Answers 2

3

In this code you did not execute the queries so you cannot fetch.

Try this approach:

<?php
    if(isset($_GET['id'], $_GET['twd'])){
        /*Importing our db connection script*/
        require_once('dbConnect.php');
        /* check connection */
        if (mysqli_connect_errno()) {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }

        $sql = sprintf("SELECT MAX(id) as MaxId FROM work_details WHERE twd = '%s'", 
                       mysqli_real_escape_string($_GET['twd']));

        if ($result = mysqli_query($con, $sql)) {
            /* fetch associative array */
            if ($row = mysqli_fetch_row($result)) {
                if($row[0] === $_GET['id']){
                    $sql = sprintf("DELETE FROM work_details WHERE id='%s';", 
                                  mysqli_real_escape_string($_GET['id']));
                    if ($result = mysqli_query($con, $sql)) {
                        echo 'success';
                    }else{
                        echo 'failed';
                    }
                }

            }

            /* free result set */
            mysqli_free_result($result);
        }
        /* close connection */
        mysqli_close($con);
    }
?>
Sign up to request clarification or add additional context in comments.

Comments

1

The error explains itself and tells you what you're doing wrong. Anyway, here's a hint: mysqli_fetch_row() does not accept the SQL string (the query) as parameter. Instead, it accepts the result that came from the DB when this query was executed and it fetches a row (a single entry) from that result.

In simpler words:

  1. You should declare the SQL query (you already have that)
  2. You should execute the query (that's the part you're missing)
  3. You should fetch row by row the results from the query result (that's the part you're mixing with step 2.)

Don't just code randomly. Try to understand what you're coding and what's going on under the hood (at least in small depth).

Here's a good and very simple example, similar to your code. You can checkout the example code snippets: http://php.net/manual/en/mysqli-result.fetch-row.php

And here's an alternative, very good object-oriented PHP module, that you can use for interacting with the DB: http://php.net/manual/en/mysqli-result.fetch-row.php

1 Comment

Thanks for your answer...I have looked through the link you gave but I still have no idea to solve no.2...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.