【问题标题】:how can i delete row from posts by user who added this post?如何从添加此帖子的用户的帖子中删除行?
【发布时间】:2017-05-09 20:58:12
【问题描述】:

这是 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);
}

}
这是删除的代码 我有两个表 1_user_info(有登录的用户) 2_posts 用户将发布它 表帖子有 user_id 外键 我想删除添加此帖子的用户的帖子,只是希望能帮助我
<?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);
?>

【问题讨论】:

  • 哪一部分不工作?...您是否使用其他工具测试了您的服务器端点http://devsinai.com/DrSiani/DeletePosts.php,例如postman

标签: php android


【解决方案1】:

试试这个解决方案 :) 您可以编辑帖子表并为每个帖子添加包含 id 的新行 查询将是

$Sql_Query = "DELETE FROM posts WHERE user_id = '$ID' and post_id='$posts_ID'";

现在您只能删除想要的帖子。

【讨论】:

  • 欢迎来到 SO。您能否格式化答案的代码/查询部分以使其更具可读性?请参阅答案窗口右上角的“帮助”链接。
  • 我不明白你想如何与其他表连接?!
  • @IbrahimHusin 没有“连接”到表这样的事情。我认为您需要在完成此项目之前完成 SQL 教程。试试这个tutorialspoint.com/mysql
猜你喜欢
  • 1970-01-01
  • 2021-03-01
  • 2021-10-19
  • 1970-01-01
  • 2019-12-01
  • 2020-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多