0
我已經成功地創建一個認證使用httppost從Java Android代碼通過PHP到MySQL。然而,我遇到問題時會創建一個新用戶。我檢查了其他用戶的多個帖子,我認爲它看起來不錯。另外,我已經創建了一個簡單的html表單來測試新用戶的php創建,並且這也起作用。httppost Android,價值不通過數據庫創建用戶
任何想法將不勝感激。這是我的Java代碼和PHP下面。謝謝!
try {
// create new array list
nameValuePairs = new ArrayList<NameValuePair>();
// place them in an array list
nameValuePairs.add(new BasicNameValuePair("username", user));
nameValuePairs.add(new BasicNameValuePair("password", pass));
// add array list to http post
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// assign executed form container to response
response = httpclient.execute(httppost);
// check status code,
if (response.getStatusLine().getStatusCode() == 200) {
// assign response entity to htpp entitiy
entity = response.getEntity();
// chekc if entity is not null
if (entity != null) {
Log.i("RESPONSE",EntityUtils.toString(entity));
Intent intent = new Intent(CreateAccount.this,
MainActivity.class);
startActivity(intent);
// }
// display a taost saying login was a success
Toast.makeText(CreateAccount.this, user,
Toast.LENGTH_SHORT).show();
// }
}
}
} catch (Exception e) {
e.printStackTrace();
// display toast when there is a connection error
Toast.makeText(getBaseContext(), "ConnectionError",
Toast.LENGTH_SHORT).show();
}
PHP代碼:
//get form data
$user = mysql_real_escape_string($_POST['user']);
$pass = mysql_real_escape_string($_POST['pass']);
echo "INSERT INTO androidlogin (user, pass) VALUES ('$user','$pass')";
//insert data
$insert = mysql_query("INSERT INTO androidlogin (user, pass) VALUES ('$user','$pass')");
if($insert){
$arr2 = array("user" => $user, "pass" => $pass);
echo json_encode($arr2);
}
非常感謝。有效!!!! – user1720683
沒問題,別忘了接受答案;) – AndacAydin