php - 如何将有值(value)的 TextView 中的值插入到mysql

标签 php android mysql

我将从 mysql 检索到的数据设置为 TextView,现在我需要从“有值(value)的 TextView ”插入值。我想我需要 2 个 php/api 来做到这一点,但我不知道如何在一项 Activity 中使用它。它成功检索到该值并将其设置为TextView,但无法将其发送到mysql。

这是我的脚本:

public class ListJawaban extends Activity {
    JSONArray mk = null;


    ProgressDialog pDialog;
    TextView idm, nama_mk, kls;
    Button submit;

    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.spin_main);

            Intent in = getIntent();
            String kode = in.getStringExtra("id_stu");
            String link_url = "http://xxxxxxxx/ffffff/gggggg/mkQuiz.php?kode="+kode;

            JSONParser jParser = new JSONParser();

            JSONObject json = jParser.AmbilJson(link_url);

            try {
                mk = json.getJSONArray("mk");

                for(int i = 0; i < mk.length(); i++){
                    JSONObject ar = mk.getJSONObject(i);

                    TextView mhs = (TextView) findViewById(R.id.nim_mhs);
                    TextView mkname = (TextView) findViewById(R.id.nama_mk);
                    TextView kelas = (TextView) findViewById(R.id.kelas);

                    String mhs_get = ar.getString("id_mhw");
                    String mkname_get = ar.getString("mk_mhw");
                    String kelas_get = ar.getString("kelas");

                    mhs.setText(mhs_get);
                    mkname.setText(mkname_get);
                    kelas.setText(kelas_get);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
submit = (Button) findViewById(R.id.submit);
            submit.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {


                        new DataMasuk().execute();
                        Intent forum = new Intent(getApplicationContext(), ForumActivity.class);                    
                        forum.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(forum);                   
                        finish();
                }
            });
        }

        public class DataMasuk extends AsyncTask<String, String, String> {

            String success;

            String url_link = "http://xxxxxxxx/ffffff/gggggg/postAns.php";

            @Override
            protected String doInBackground(String... params) {

                String strnama = nama_mk.getText().toString();
                String stremail = idm.getText().toString();
                String strpassword = kls.getText().toString();

                List<NameValuePair> nvp = new ArrayList<NameValuePair>();
                nvp.add(new BasicNameValuePair("mk_mhs", strnama));
                nvp.add(new BasicNameValuePair("id_mhs", stremail));
                nvp.add(new BasicNameValuePair("kelas", strpassword));

                JSONParser jParser = new JSONParser();

                JSONObject json = jParser.makeHttpRequest(url_link, "POST", nvp);
                try {
                    success = json.getString("success");

                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "Error",
                            Toast.LENGTH_LONG).show();
                }

                return null;
            }



        }
 }

这是插入值的 php

<?php

$id_mhs=$_POST['id_mhs'];
$mk_mhs = $_POST['mk_mhs'];
$kelas=$_POST['kelas'];

include "koneksi.php";


$query = "INSERT INTO tbl_tampung (id_mhs,mk_mhs,kelas)VALUES('$id_mhs','$mk_mhs','$kelas')";
$hasil = mysql_query($query);
if($hasil)
    {
    $response["success"] = "1";
        $response["message"] = "Successfully Added";
        echo json_encode($response);
    }
    else
    {$response["success"] = "0";
     $response["message"] = "Sorry somethings wrong";

        echo json_encode($response);
    }
?>

但是不行,谁能帮帮我?这是我的作业,应该在星期四完成:(

最佳答案

尝试改变

        submit = (Button) findViewById(R.id.submit);
        submit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                    new DataMasuk().execute();

            }
        });

        @Override
        protected String doInBackground(String... params) {

            String strnama = nama_mk.getText().toString();
            String stremail = idm.getText().toString();
            String strpassword = kls.getText().toString();

            List<NameValuePair> nvp = new ArrayList<NameValuePair>();
            nvp.add(new BasicNameValuePair("mk_mhs", strnama));
            nvp.add(new BasicNameValuePair("id_mhs", stremail));
            nvp.add(new BasicNameValuePair("kelas", strpassword));

            JSONParser jParser = new JSONParser();

            JSONObject json = jParser.makeHttpRequest(url_link, "POST", nvp);
            try {
                success = json.getInt("success");
                if (success == 1) {

                    Intent forum = new Intent(getApplicationContext(), ForumActivity.class);                    
                    forum.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(forum);                   
                    finish();
                }

            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Error",
                        Toast.LENGTH_LONG).show();
            }

            return null;
        }



    }

关于php - 如何将有值(value)的 TextView 中的值插入到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29495991/

相关文章:

mysql - 使用左连接和连接表的另一个字段中的位置

php - 发送抓取请求以获取 torrent 的种子和同行

android - 如果未安装应用程序,从 webview 调用 facebook sdk 不起作用?

android - Xamarin Forms WebView 地理位置问题

android - Android 开发的 Macbook Air 硬件要求?

mysql root密码忘记了

mysql - SQL 语句出现错误

php - 解析编码 $_SERVER ['request_uri' ]

php - 拉拉维尔 : How do I get records from pivot table whereby its foreign key does not reference a primary key?

php - JSTree:使用 php 和 mysql 创建可扩展的树结构