字段没有插入mysql数据库android

时间:2017-08-31 11:18:50

标签: php android mysql

enter image description here我试图将一个Android表单数据存储到MySQL数据库中,但事情是其他字段完全正常,它们的值存储到DB中,唯一的名称和电子邮件不会插入到数据库中。所有代码都没问题,我甚至没有收到任何错误。请告诉我我做错了什么。如果你能看到图片,有些领域的价值为0.我不明白为什么。如果你知道,请告诉我一个合适的解决方案

这是我的代码 的 MainActivity.java

public class MainActivity extends AppCompatActivity {

    private EditText etid, etname, etstorename, etemail, etphone, etdoj;
    Button btnregister;
    TextView tvlogin;
    private ParseContent parseContent;
    PreferenceHelper preferenceHelper;
    private final int RegTask = 5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        preferenceHelper = new PreferenceHelper(this);
        parseContent = new ParseContent(this);

        if(preferenceHelper.getIsLogin()){
            Intent intent = new Intent(MainActivity.this,WelcomeActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            this.finish();
        }
        etid = (EditText) findViewById(R.id.etmi_id);
        etname = (EditText) findViewById(R.id.etname);
        etstorename = (EditText) findViewById(R.id.etstore_name);
        etemail = (EditText) findViewById(R.id.etemail);
        etphone = (EditText) findViewById(R.id.etphone);
        etdoj = (EditText) findViewById(R.id.etdoj);
        btnregister = (Button) findViewById(R.id.btn);
        tvlogin = (TextView) findViewById(R.id.tvlogin);

        tvlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });

        btnregister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    register();
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private void register() throws IOException, JSONException {

        if (!AndyUtils.isNetworkAvailable(MainActivity.this)) {
            Toast.makeText(MainActivity.this, "Internet is required!", Toast.LENGTH_SHORT).show();
            return;
        }
        AndyUtils.showSimpleProgressDialog(MainActivity.this);
        final HashMap<String, String> map = new HashMap<>();
        map.put(AndyConstants.Params.MI_ID, etid.getText().toString());
        map.put(AndyConstants.Params.NAME, etname.getText().toString());
        map.put(AndyConstants.Params.STORE_NAME, etstorename.getText().toString());
        map.put(AndyConstants.Params.EMAIL, etemail.getText().toString());
        map.put(AndyConstants.Params.PHONE, etphone.getText().toString());
        map.put(AndyConstants.Params.DOJ, etdoj.getText().toString());
        new AsyncTask<Void, Void, String>(){
            protected String doInBackground(Void[] params) {
                String response="";
                try {
                    HttpRequest req = new HttpRequest(AndyConstants.ServiceType.REGISTER);
                    response = req.prepare(HttpRequest.Method.POST).withData(map).sendAndReadString();
                } catch (Exception e) {
                    response=e.getMessage();
                }
                return response;
            }
            protected void onPostExecute(String result) {
                //do something with response
                Log.d("newwwss", result);
                onTaskCompleted(result, RegTask);
            }
        }.execute();
    }

    private void onTaskCompleted(String response,int task) {
        Log.d("responsejson", response.toString());
        AndyUtils.removeSimpleProgressDialog();  //will remove progress dialog
        switch (task) {
            case RegTask:

                if (parseContent.isSuccess(response)) {

                    parseContent.saveInfo(response);
                    Toast.makeText(MainActivity.this, "Registered Successfully!", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(MainActivity.this,WelcomeActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    this.finish();

                }else {
                    Toast.makeText(MainActivity.this, parseContent.getErrorMessage(response), Toast.LENGTH_SHORT).show();
                }
        }
    }
}

PreferenceHelper.java

public class PreferenceHelper {
    private final String MI_ID = "mi_id";
    private final String NAME = "name";
    private final String STORE_NAME = "store_name";
    private final String EMAIL = "email";
    private final String PHONE = "phone";
    private final String DOJ = "doj";
    private SharedPreferences app_prefs;
    private Context context;

    public PreferenceHelper(Context context) {
        app_prefs = context.getSharedPreferences("shared",
                Context.MODE_PRIVATE);
        this.context = context;
    }

    public void putIsLogin(boolean loginorout) {
        SharedPreferences.Editor edit = app_prefs.edit();
        edit.putBoolean(PHONE, loginorout);
        edit.apply();
    }
    public boolean getIsLogin() {
        return app_prefs.getBoolean(PHONE, false);
    }

    public void putMi_id(String loginorout) {
        SharedPreferences.Editor edit = app_prefs.edit();
        edit.putString(MI_ID, loginorout);
        edit.apply();
    }
    public String getMi_id() {
        return app_prefs.getString(MI_ID, "");
    }

    public void putName(String loginorout) {
        SharedPreferences.Editor edit = app_prefs.edit();
        edit.putString(NAME, loginorout);
        edit.apply();
    }
    public String getName() {
        return app_prefs.getString(NAME, "");
    }

    public void putStore_name(String loginorout) {
        SharedPreferences.Editor edit = app_prefs.edit();
        edit.putString(STORE_NAME, loginorout);
        edit.apply();
    }
    public String getStore_name() {
        return app_prefs.getString(STORE_NAME, "");
    }

    public void putEmail(String loginorout) {
        SharedPreferences.Editor edit = app_prefs.edit();
        edit.putString(EMAIL, loginorout);
        edit.apply();
    }
    public String getEmail() {
        return app_prefs.getString(EMAIL, "");
    }
    public void putPhone(String loginorout) {
        SharedPreferences.Editor edit = app_prefs.edit();
        edit.putString(PHONE, loginorout);
        edit.apply();
    }
    public String getPhone() {
        return app_prefs.getString(PHONE, "");
    }

    public void putDoj(String loginorout) {
        SharedPreferences.Editor edit = app_prefs.edit();
        edit.putString(DOJ, loginorout);
        edit.commit();
    }
    public String getDoj() {
        return app_prefs.getString(DOJ, "");
    }
 }

Simplereg.php

<?php

   if($_SERVER['REQUEST_METHOD']=='POST'){
  // echo $_SERVER["DOCUMENT_ROOT"];  // /home1/demonuts/public_html
 //including the database connection file
       include_once("dbConfig.php");
        $mi_id = $_POST['mi_id'];
        $name = $_POST['name'];
        $store_name = $_POST['store_name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $doj = $_POST['doj'];


     if($mi_id == '' || $name == '' || $store_name == '' || $email == '' || $phone == '' ||$doj == ''){
            echo json_encode(array( "status" => "false","message" => "Parameter missing!") );
     }else{

            $query= "SELECT * FROM registerdemo WHERE name='$name'";
            $result= mysqli_query($con, $query);

            if(mysqli_num_rows($result) < 1){
               echo json_encode(array( "status" => "false","message" => "Username already exist!") );
            }else{ 
            $name = $_POST['name'];

             $query1 = "INSERT INTO registerdemo (mi_id,name,store_name,email,phone,doj) VALUES ('$mi_id','$name','$store_name','$email','$phone','$doj')";
             if(mysqli_query($con,$query1)){

                 $query2= "SELECT * FROM registerdemo WHERE name='$name'";
                         $result2= mysqli_query($con, $query2);
                     $emparray = array();
                         if(mysqli_num_rows($result2) > 0){  
                         while ($row = mysqli_fetch_assoc($result2)!=NULL) {
                                     $emparray[] = $row;
                                   }
                         } 
                echo json_encode(array( "status" => "true","message" => "Successfully registered!" , "data" => $emparray) );
             }else{
                 echo json_encode(array( "status" => "false","message" => "Error occured, please try again!") );
            }
        }
     }
     } else{
            echo json_encode(array( "status" => "false","message" => "Error occured, please try again!") );
    }

 ?>

0 个答案:

没有答案