为什么在Android中的MySQL表中插入空值?

时间:2014-04-11 16:20:38

标签: php android mysql

我是android新手。我正在创建一个应用程序,其中有一个注册表单,从那里输入值需要存储在MySQL表中。

这是java的源代码:

package com.example.entrepreneurexpress;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class InvestorRegister extends Activity {

    EditText YourName;
    EditText email;
    EditText password;
    EditText confirmPassword;
    Button btnClear, btnRegister;

    String nm, emailAdd, cnfPass, pwd, result = null;
    int code;

    InputStream is = null;


    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.investors_registration);

        if(Build.VERSION.SDK_INT >= 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        ActionBar aBar = getActionBar();
        aBar.setDisplayHomeAsUpEnabled(true);

        YourName = (EditText) findViewById(R.id.invRegName);
        email = (EditText) findViewById(R.id.invRegEmail);
        password = (EditText) findViewById(R.id.invRegPassword);
        confirmPassword = (EditText) findViewById(R.id.invRegConfPassword);

        btnRegister = (Button) findViewById(R.id.btnInvRegRegister);
        btnRegister.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                if(
                    (YourName.getText().toString().trim().equals("")) ||
                    (email.getText().toString().trim().equals("")) ||
                    (password.getText().toString().trim().equals(""))
                  ) {
                    Toast.makeText(getApplicationContext(), "Please Fill In All The Details", Toast.LENGTH_LONG).show();
                } else {
                    nm = YourName.getText().toString().trim();
                    emailAdd = email.getText().toString().trim();
                    pwd = password.getText().toString().trim();

                    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair(nm, email.getText().toString().trim()));
                    nameValuePairs.add(new BasicNameValuePair(emailAdd, email.getText().toString().trim()));
                    nameValuePairs.add(new BasicNameValuePair(pwd, password.getText().toString().trim()));

                     try{
                         HttpClient httpclient = new DefaultHttpClient();
                         HttpPost httppost = new HttpPost("http://www.bijukathemovie.com/insert.php");
                         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                         HttpResponse response = httpclient.execute(httppost);
                         HttpEntity entity = response.getEntity();
                         is = entity.getContent();

                         Log.e("log_tag", "connection success ");
                         Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
                     } catch(Exception e) {
                        Log.e("log_tag", "Error in http connection "+e.toString());
                        Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
                     }

                     try{
                         BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                         StringBuilder sb = new StringBuilder();
                         String line = null;
                         while ((line = reader.readLine()) != null) {
                             sb.append(line + "\n");                                
                         }
                         is.close();
                         result=sb.toString();
                     } catch(Exception e) {
                        Log.e("log_tag", "Error converting result "+e.toString());
                     }

                     try {
                         JSONObject json_data = new JSONObject(result);
                         CharSequence w = (CharSequence) json_data.get("re");
                         Toast.makeText(getApplicationContext(), w, Toast.LENGTH_SHORT).show();
                     } catch (JSONException e) {
                         Log.e("log_tag", "Error parsing data "+e.toString());
                         Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
                     }
                }
            }
        });
    }
}

这里是php的源代码:

<?php

require_once("storescripts/dbconnect.php");

    $name=$_POST['nm'];
    $emailAdd=$_POST['emailAdd'];
    $pwd=$_POST['pwd'];

    //echo  "Your Name is: ". $name .". Your Email is: ". $emailAdd .". Your Password is:  ". $pwd;

    $q="INSERT INTO Investors VALUES('".$name."', '".$emailAdd."', '".$pwd."')";
    $s= mysqli_query($con, $q);

    if(!$s) {
        $r["re"]="Inserting problem in database";
        print(json_encode($r));
    } else {
        $r["re"]="Record inserted successfully";
        print(json_encode($r));
    }

mysqli_close($con);
?>

您可以看到图片here

数据库中的空白值除了id字段auto_increment。

请帮助我解决这个问题。感谢。

1 个答案:

答案 0 :(得分:0)

你在参数中犯了一些错误

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair(nm, email.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair(emailAdd, email.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair(pwd, password.getText().toString().trim()));

应该是:

nm = YourName.getText().trim();
emailAdd = email.getText().trim();
pwd = password.getText().trim();

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("nm", nm));
nameValuePairs.add(new BasicNameValuePair("emailAdd", email));
nameValuePairs.add(new BasicNameValuePair("pwd", pwd));

检查documentation

  

BasicNameValuePair(String name,String value)

     

使用名称和值的默认构造函数。