我想選擇在MySQL中的行值。 當我運行的代碼時,logcat的節目「必填字段(一個或多個)丟失」。 我不知道在PHP或Android代碼的問題。 我希望有人能幫助我。爲什麼PHP不能得到的價值?在android系統到PHP
public class profileActivity extends Activity{
EditText txtName;
EditText inputusername;
EditText answer;
EditText txtPrice;
EditText txtDesc;
EditText txtCreatedAt;
String username;
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_FIRSTNAME = "first_name";
private static String KEY_lASTNAME = "last_name";
private static String KEY_EMAIL = "email";
private DatabaseHandler dbHelper;
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// single product url
private static final String profileURL = "http://10.0.2.2/android_login_api4/include/profile.php";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
// getting product details from intent
Intent i = getIntent();
// getting product id (pid) from intent
username = i.getStringExtra(KEY_NAME);
// Getting complete product details in background thread
new GetProfileDetails().execute();
}
/**
* Background Async Task to Get complete product details
* */
class GetProfileDetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(profileActivity.this);
pDialog.setMessage("Loading product details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting product details in background thread
* */
protected String doInBackground(String... params) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Check for success KEY
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.getJSONFromUrl(
profileURL, params);
// check your log for json response
Log.d("Single Product Details", json.toString());
// json success KEY
success = json.getInt(KEY_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray productObj = json
.getJSONArray(KEY_NAME); // JSON Array
// get first product object from JSON Array
JSONObject product = productObj.getJSONObject(0);
// product with this uid found
// Edit Text
txtName = (EditText) findViewById(R.id.name);
txtPrice = (EditText) findViewById(R.id.email);
txtDesc = (EditText) findViewById(R.id.tel);
// display product data in EditText
txtName.setText(product.getString(KEY_lASTNAME));
txtPrice.setText(product.getString(KEY_FIRSTNAME));
txtDesc.setText(product.getString(KEY_EMAIL));
}else{
// product with uid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
}
這是PHP
<?php
// array for JSON response
$response = array();
// include db connect class
require_once 'DB_Connect.php';
// connecting to db
$db = new DB_CONNECT();
// check for post data
if (isset($_GET["username"])) {
$username = $_GET['username'];
// get a product from products table
$result = mysql_query("SELECT *FROM users WHERE username = $username");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$product = array();
$product["username"] = $result["username"];
$product["first_name"] = $result["first_name"];
$product["last_name"] = $result["last_name"];
$product["email"] = $result["email"];
$product["tel"] = $result["tel"];
$product["age"] = $result["age"];
// success
$response["success"] = 1;
// user node
$response["product"] = array();
array_push($response["product"], $product);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
我假設'$ username'是一個字符串?它必須在查詢中引用爲''$ username''_ _ _ _ _ _ _ _ _ _ _ _之後,您已經調用_'mysql_real_escape_string()'來防止sql注入。 – 2013-02-26 13:56:57
如果你纔剛剛開始這個項目,它是不是太晚了改變。不要使用'mysql_query()'及其親屬,而應考慮切換到支持預備語句(如[PDO](http://php.net/manual/en/book.pdo.php)或MySQLi)的API。使用得當,他們更安全。 – 2013-02-26 14:02:51