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


How to compare value of a variable from Android and MySQL?

我正在尝试比较Android中EditText的值,并将其与MySQL和PHP的值进行比较。

这是我的java代码:

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

这是我的PHP代码:

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


?>

没有错误,但它总是显示"无效代码",尽管输入的代码是正确的。我该如何解决这个问题?

请将代码更改为:

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

试试这个,问题是$smsCode = isset($_POST['smsCode']);,返回应该重写的boolean,如下所示。

isset—确定变量是否已设置并且不是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";
    }