doukai2839
2015-03-04 04:29
浏览 33

如何比较Android和MySQL变量的值?

I am trying to compare the value from EditText in Android and compare it with a value from MySQL with PHP.

Here is my java code:

public class DisplayInformation extends MainActivity {
    private Button confirmCode;
    private ProgressDialog pDialog;
    JSONParser jsonParser = new JSONParser();
    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient;
    EditText smsCode;
    List<NameValuePair> nameValuePairs;
    private static final String LOGIN_URL = "http://10.0.2.2/callarocket/confirmedRequest.php";
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_screen);
        TextView textView1 = (TextView)findViewById(R.id.textView1);
        TextView textView2 = (TextView)findViewById(R.id.textView2);
        TextView textView3 = (TextView)findViewById(R.id.textView3);
        TextView textView4 = (TextView)findViewById(R.id.textView4);
        TextView textView5 = (TextView)findViewById(R.id.textView5);

        textView1.setText(getIntent().getStringExtra("displayName"));
        textView2.setText(getIntent().getStringExtra("displayContact"));
        textView3.setText(getIntent().getStringExtra("displayAddress"));
        textView4.setText(getIntent().getStringExtra("displayStore"));
        textView5.setText(getIntent().getStringExtra("displayRequest"));

        smsCode = (EditText) findViewById(R.id.smscode);

        confirmCode = (Button)findViewById(R.id.confirmsmscode);
        //confirmCode.setOnClickListener(this);
        confirmCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pDialog = ProgressDialog.show(DisplayInformation.this, "",
                        "Validating Code...", true);
                new Thread(new Runnable() {
                    public void run() {
                        login();
                    }
                }).start();
            }
        });
    }

   // public void onClick(View v) {
        // TODO Auto-generated method stub
        //new CreateUser().execute();


    //}

    public void login(){
        try{

            httpclient=new DefaultHttpClient();
            httppost= new HttpPost("http://10.0.2.2/callarocket/check_code.php"); // make sure the url is correct.
            //add your data
            nameValuePairs = new ArrayList<NameValuePair>(1);
            // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
            //int smsNum = Integer.parseInt(smsCode.getText().toString());

            nameValuePairs.add(new BasicNameValuePair("smsCode",smsCode.getText().toString()));  // $Edittext_value = $_POST['Edittext_value'];
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            //Execute HTTP Post Request
            response=httpclient.execute(httppost);

            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);
                    pDialog.dismiss();
                }
            });

            if(response.equalsIgnoreCase("Code Found")){
                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(DisplayInformation.this,"Code Matched", Toast.LENGTH_SHORT).show();
                    }
                });

                //startActivity(new Intent(AndroidPHPConnectionDemo.this, UserPage.class));
            }else{
                showAlert();
            }

        }catch(Exception e){
            pDialog.dismiss();
            System.out.println("Exception : " + e.getMessage());
        }
    }
    public void showAlert(){
        DisplayInformation.this.runOnUiThread(new Runnable() {
            public void run() {
                AlertDialog.Builder builder = new AlertDialog.Builder(DisplayInformation.this);
                builder.setTitle("Error.");
                builder.setMessage("Invalid Code.")
                        .setCancelable(false)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();
            }
        });
    }
  }

And here is my PHP code:

<?php 
    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

    //$smsCode = 8044;
    $smsCode = isset($_POST['smsCode']);
    $query_search = "SELECT * FROM userrequests WHERE smsCode='".$smsCode."'";
    $query_exec = mysqli_query($db->getConnection(),$query_search) or die(mysqli_query());
    $row = mysqli_num_rows($query_exec);
    //echo $rows;
     if($row == 0) { 
        echo "Invalid SMS Code"; 
     }
     else  {
     //Authentication code is correct
        echo "Code matched"; 
    }





?>

There is no error but it always show "Invalid Code" although the code entered is correct. How do I fix this ?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongnuo6310 2015-03-04 04:51
    已采纳

    Please change code as :

     if(response.equalsIgnoreCase("Code matched")){
                    runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(DisplayInformation.this,"Code Matched", Toast.LENGTH_SHORT).show();
                        }
                    });
    
                    //startActivity(new Intent(AndroidPHPConnectionDemo.this, UserPage.class));
                }else{
                    showAlert();
                }
    
    已采纳该答案
    打赏 评论
  • dongzhuo1733 2015-03-04 04:39

    Try this, Problem is $smsCode = isset($_POST['smsCode']);, returns boolean That should be rewritten as shown in below.

    isset — Determine if a variable is set and is not NULL

        if(isset($_POST['smsCode'])){
                $smsCode = $_POST['smsCode'];
                $query_search = "SELECT * FROM userrequests WHERE smsCode='".$smsCode."'";
                $query_exec = mysqli_query($db->getConnection(),$query_search) or die(mysqli_error($db->getConnection()));
                $row = mysqli_num_rows($query_exec);
                //echo $rows;
                if($row == 0) {
                    echo "Invalid SMS Code";
                }
                else  {
                    //Authentication code is correct
                    echo "Code matched";
                }
        }else{
            echo "Empty Code";
        }
    
    打赏 评论

相关推荐 更多相似问题