这是android studio里面的代码,我希望从帖子中删除,只是用户通过外键user_id从表用户添加此帖子,该用户引用注册用户
public class DailogeUbdatePost extends Dialog implements View.OnClickListener {
String HttpUrlDeleteRecord = "http://localhost/DrSiani/DeletePosts.php";
public Context c;
public Dialog d;
public Button deletDialog,ubdateDialoge;
TextView textDialogAdd;
ProgressDialog progressDialog2;
HashMap<String,String> hashMap = new HashMap<>();
String finalResult ;
HttpParse httpParse = new HttpParse();
public DailogeUbdatePost(Context a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_dailoge_ubdate_post);
deletDialog= (Button) findViewById(R.id.deletDialog);
ubdateDialoge= (Button) findViewById(R.id.ubdateDialoge);
deletDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StudentDelete(finalResult);
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.BackBtnDialogDR:
dismiss();
break;
default:
break;
}
dismiss();
}
public void StudentDelete(final String uerId) {
class DeletePosts extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog2 = ProgressDialog.show(getContext(), "Loading Data", null, true, true);
}
@Override
protected void onPostExecute(String httpResponseMsg) {
super.onPostExecute(httpResponseMsg);
progressDialog2.dismiss();
Toast.makeText(getContext(), httpResponseMsg.toString(), Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(String... params) {
hashMap.put("user_id", params[0]);
finalResult = httpParse.postRequest(hashMap, HttpUrlDeleteRecord);
return finalResult;
}
}
DeletePosts deletePosts = new DeletePosts();
deletePosts.execute(uerId);
}
}
&#13;
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
include 'connecttomysql.php';
$con = mysqli_connect($dbserver,$dbusername,$dbpassword,$dbname);
$ID = $_POST['user_id'];
$Sql_Query = "DELETE FROM posts WHERE user_id = '$ID'";
if(mysqli_query($con,$Sql_Query))
{
echo 'Record Deleted Successfully';
}
else
{
echo 'Something went wrong';
}
}
mysqli_close($con);
?>
答案 0 :(得分:1)
试试这个解决方案:) 您可以编辑posts表并为每个帖子添加新行包含id 并且查询将是
$Sql_Query = "DELETE FROM posts WHERE user_id = '$ID' and post_id='$posts_ID'";
现在你只能删除想要的帖子。