从Android的输入数据到MySQL数据库 [英] Input data from android to database in mysql

查看:115
本文介绍了从Android的输入数据到MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code:

<?php include('konek.php');

$entid=$_POST['entId'];
$entSender=$_POST['entSender'];
$entTitle=$_POST['entTitle'];
$entDate=$_POST['entDate'];
$entSAGrade=$_POST['entSAGrade'];
$entReason=$_POST['entReason'];
$entProblem=$_POST['entProblem'];
$entTime=$_POST['entTime'];

if($row_num != 0)
{
$ins=mysql_query("INSERT into tblentry(entId,entSender,entTitle,entTime,entSAGrade,entReason,entProblem) VALUES('$entId','$entSender','$entTitle',CURTIME(),'$entSAGrade','$entReason','$‌​entProblem')");
echo "Added"; }
else 
{   echo "Added."; }
?>

我的问题是,当我输入我的Andr​​oid模拟器的数据,不加入。在我的code中的问题是什么?

My problem is, when i input the data in my android simulator, it is not adding. What is the problem in my code?

这是我的code在android系统:

And here is my code in android:

public class AddEntry extends Activity {

Button buttonSave;
EditText ent1, ent2, ent3, ent4, ent5, ent6 ;
HttpPost httppost;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addentry);

    buttonSave = (Button)findViewById(R.id.Save);  
    ent1 = (EditText)findViewById(R.id.entSender);
    ent2 = (EditText)findViewById(R.id.entDate);
    ent3 = (EditText)findViewById(R.id.entTitle);
    ent4 = (EditText)findViewById(R.id.entSAGrade);
    ent5 = (EditText)findViewById(R.id.entReason);
    ent6 = (EditText)findViewById(R.id.entProblem);

    buttonSave.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog = ProgressDialog.show(AddEntry.this, "", 
                    "Adding entry. . .", true);
             new Thread(new Runnable() {
                    public void run() {
                        insertEntry();

                    }

                  }).start();               
        }
    });

}

void insertEntry(){
    try{            

        httpclient=new DefaultHttpClient();
        httppost= new HttpPost("http://10.0.2.2/smcfi/insertentry.php"); // make sure the url is correct.
        //add your data
        nameValuePairs = new ArrayList<NameValuePair>(6);
        // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar, 
        nameValuePairs.add(new BasicNameValuePair("enSender",ent1.getText().toString().trim()));  // $Edittext_value = $_POST['Edittext_value'];
        nameValuePairs.add(new BasicNameValuePair("entDate",ent2.getText().toString().trim()));
        nameValuePairs.add(new BasicNameValuePair("entTitle",ent3.getText().toString().trim()));
        nameValuePairs.add(new BasicNameValuePair("entSAGrade",ent4.getText().toString().trim()));
        nameValuePairs.add(new BasicNameValuePair("entReason",ent5.getText().toString().trim()));
        nameValuePairs.add(new BasicNameValuePair("entProblem",ent6.getText().toString().trim()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        //Execute HTTP Post Request
        response=httpclient.execute(httppost);
        // edited by James from coderzheaven.. <span id="IL_AD10" class="IL_AD">from here</span>....
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        final String response = httpclient.execute(httppost, responseHandler);
        System.out.println("Response : " + response); 
        runOnUiThread(new Runnable() {
            public void run() {
               // tv.setText("Response from PHP : " + response);
                dialog.dismiss();
            }
        });

        if(response.equalsIgnoreCase("Added")){
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(AddEntry.this,"Successfully Added.", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(AddEntry.this, Entry.class);
                    AddEntry.this.startActivity(intent);
                    AddEntry.this.finish();
                }
            });

        }else{
            showAlert();                
        }

    }catch(Exception e){
        dialog.dismiss();
        System.out.println("Exception : " + e.getMessage());
    }
}
public void showAlert(){
    AddEntry.this.runOnUiThread(new Runnable() {
        public void run() {
            AlertDialog.Builder builder = new AlertDialog.Builder(AddEntry.this);
            builder.setTitle("Error");
            builder.setMessage("Not Added.")  
                   .setCancelable(false)
                   .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                       }
                   });                     
            AlertDialog alert = builder.create();
            alert.show();
        }
    });
}
@Override
public void onBackPressed() {
    Intent intent = new Intent(AddEntry.this, MainAct.class);
    AddEntry.this.startActivity(intent);
    AddEntry.this.finish();
}

}

我不知道什么是错误。但它不加入。我输入的数据不加入到我的数据库。

I dont know what is the error. but it is not adding. my inputted data is not adding to my database.

推荐答案

首先,你应该测试你的PHP服务分离,例如使用Chrome的邮差扩展。
当你确保PHP正常工作,下一步是Android的code连接到Web服务。

First you should test your PHP service isolated, e.g. using Chrome's PostMan extension. As you make sure PHP works properly, next step is to connect Android's code to your web service.

请检查邮递员一个不错的调试;)

Please check PostMan for a nice debugging ;)

<一个href=\"https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en\" rel=\"nofollow\">https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en

这篇关于从Android的输入数据到MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆