php - 从android输入数据到mysql数据库

标签 php android mysql

我有这个代码:

<?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."; }
?>

我的问题是,当我在 Android 模拟器中输入数据时,它没有添加。我的代码有什么问题?

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

}

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

最佳答案

发现拼写错误 -

 $ins=mysql_query("INSERT into tblentry
     (entId,entSender,entTitle,entTime,entSAGrade,entReason,entProblem)
     VALUES
     ('$entId','$entSender','$entTitle',CURTIME(),'$entSAGrade','$entReason','$entProblem'));"
                                                                                    ^=== This should be inside round bracket


更改为-

$ins=mysql_query("INSERT into tblentry
                 (entId,entSender,entTitle,entTime,entSAGrade,entReason,entProblem)
                 VALUES
                 ('$entId','$entSender','$entTitle',CURTIME(),'$entSAGrade','$entReason','$entProblem')");

关于php - 从android输入数据到mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23225652/

相关文章:

php - mysql db 对所有主机连接开放

android - 检测 Fling 距离 android

java - ScrollView 不与 LinearLayout 一起使用,打开键盘时 View 有重量?

mysql - 如何将索引从一个相同的表复制到另一个现有的相同表

mysql - 在 Ubuntu 19.10 上安装后无法登录 mysql

php - 对大数据执行 AJAX 搜索的更好方法

php - mysqli 插入执行两次

php - 使用 $http.get 从 sql 数据库检索数据

php - 如何访问 Symfony 2 中的 bundle 实例?

java - 如何为 ListView 创建自定义光标适配器以用于图像和文本?