基本上在我的Android应用程序中,需要用户填写2个字段。一个是必须的,另一个是可选的。
我的PHP脚本:
<?php
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
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";
$userName = $_POST['userName'];
$userContact = $_POST['userContact'];
$userAddress = $_POST['userAddress'];
$userStore = $_POST['userStore'];
$userRequest = $_POST['userRequest'];
$userTime = $_POST['userTime'];
$sql_insert = "INSERT INTO confirmedrequest(userName,smsCode,contactNumber,userAddress,storeList,timeFrame,requestBody) VALUES ('$userName','$smsCode','$userContact','$userAddress','$userStore','$userTime','$userRequest')";
$sql_exec = mysqli_query($db->getConnection(),$sql_insert) or die(mysqli_error($db->getConnection()));
if(isset($_POST['promoCode'])){
$promoCode = $_POST['promoCode'];
$query_search1 = "SELECT * FROM promocodetable WHERE promoCode='".$promoCode."'";
$query_exec1 = mysqli_query($db->getConnection(),$query_search1) or die(mysqli_error($db->getConnection()));;
$row1 = mysqli_num_rows($query_exec1);
if($row1==0){
//do nothing
}else{
echo "Promo code and sms code matched";
$userName = $_POST['userName'];
$userContact = $_POST['userContact'];
$userAddress = $_POST['userAddress'];
$userStore = $_POST['userStore'];
$userRequest = $_POST['userRequest'];
$userTime = $_POST['userTime'];
$promoCode = $_POST['promoCode'];
$smsCode = $_POST['smsCode'];
$sql_insert1 = "INSERT INTO promorequest(userName,smsCode,contactNumber,userAddress,storeList,timeFrame,requestBody,promoCode,smsCode) VALUES ('$userName','$smsCode','$userContact','$userAddress','$userStore','$userTime','$userRequest','$promoCode','$smsCode')";
$sql_exec1 = mysqli_query($db->getConnection(),$sql_insert1) or die(mysqli_error($db->getConnection()));
}
}else{
echo "Code matched";
}
}
}else{
echo "Empty Code";
}
这是我的java代码:
httpclient=new DefaultHttpClient();
httppost= new HttpPost("www.something.com/something.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()));
nameValuePairs.add(new BasicNameValuePair("promoCode",promoCode.getText().toString()));// $Edittext_value = $_POST['Edittext_value'];
nameValuePairs.add(new BasicNameValuePair("userName",confirmName));
nameValuePairs.add(new BasicNameValuePair("userContact",confirmContact));
nameValuePairs.add(new BasicNameValuePair("userAddress",confirmAddress));
nameValuePairs.add(new BasicNameValuePair("userStore",confirmStore));
nameValuePairs.add(new BasicNameValuePair("userRequest",confirmRequest));
nameValuePairs.add(new BasicNameValuePair("userTime",confirmTime));
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);
runOnUiThread(new Runnable() {
public void run() {
Log.i("PHP Response: ", response);
pDialog.dismiss();
}
});
if(response.equalsIgnoreCase("Promo code and sms code matched")) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(DisplayInformation.this, "Promo Code Matched", Toast.LENGTH_SHORT).show();
Intent myintent = new Intent(DisplayInformation.this, Confirmation.class);
myintent.putExtra("confirmID", smsCode.getText().toString());
myintent.putExtra("confirmPromo",promoCode.getText().toString());
myintent.putExtra("displayName", confirmName);
myintent.putExtra("displayContact", confirmContact);
myintent.putExtra("displayAddress", confirmAddress);
myintent.putExtra("displayStore", confirmStore);
myintent.putExtra("displayTime", confirmTime);
myintent.putExtra("displayRequest", confirmRequest);
myintent.putExtra("displayFee", String.valueOf(deliveryFee));
startActivity(myintent);
}
});
}
if(response.equalsIgnoreCase("Code matched")){
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(DisplayInformation.this,"Code Matched", Toast.LENGTH_SHORT).show();
Intent myintent = new Intent(DisplayInformation.this, Confirmation.class);
myintent.putExtra("confirmID",smsCode.getText().toString());
myintent.putExtra("displayName",confirmName);
myintent.putExtra("displayContact",confirmContact);
myintent.putExtra("displayAddress",confirmAddress);
myintent.putExtra("displayStore",confirmStore);
myintent.putExtra("displayTime",confirmTime);
myintent.putExtra("displayRequest",confirmRequest);
myintent.putExtra("displayFee",String.valueOf(deliveryFee));
startActivity(myintent);
}
});
//startActivity(new Intent(AndroidPHPConnectionDemo.this, UserPage.class));
}else{
showAlert();//testing bitbuckettt
}
?>
问题是,当smsCode正确或不正确时,应用程序始终显示警告消息“无效代码”。 当正确输入smsCode但应用程序始终显示“无效代码”时,它能够将值插入数据库。 Log.i没有显示任何价值。 该应用没有错误。
如果用户在没有promoCode的情况下输入了正确的smsCode,我希望它转到第二个if语句if(response.equalsIgnoreCase("Code matched"))
。如果用户输入了正确的smsCode和promoCode,我希望它转到第一个if语句if(response.equalsIgnoreCase("Promo code and sms code matched"))
。但它现在不起作用。