我是php
的新手,因为给定了特定的ID,我很难找到max(id)
。
在活动A 中,有一个listView
。当列表检测到长按时,它将检查id
是否为max id
。如果是,则会删除,否则会显示列表无法删除。
这就是我的尝试。
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);
}
?>
当我在确认对话框中单击是时,我收到错误。
错误
有人可以帮忙吗?感谢
答案 0 :(得分:3)
在此代码中,您没有执行查询,因此无法获取。
尝试这种方法:
<?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);
}
?>
答案 1 :(得分:1)
错误解释了自己并告诉你你做错了什么。无论如何,这里有一个提示:mysqli_fetch_row()
不接受SQL字符串(查询)作为参数。相反,它接受执行此查询时来自DB的结果,并从该结果中获取行(单个条目)。
简单来说:
不要只是随机编码。尝试了解你正在编码的内容以及幕后发生的事情(至少在深度上)。
这是一个很好且非常简单的示例,类似于您的代码。您可以查看示例代码段:http://php.net/manual/en/mysqli-result.fetch-row.php
这是一个替代的,非常好的面向对象的PHP模块,可用于与数据库进行交互:http://php.net/manual/en/mysqli-result.fetch-row.php