我擁有將我的應用程序鏈接到我的sql數據庫的所有代碼logcat中沒有錯誤,應用程序不崩潰,並且在「數據輸入」的吐司消息在成功出現所有捕獲條款,但在數據庫表中沒有任何類型的數據輸入。在mysql數據庫中沒有輸入數據
這裏是MainActivity代碼:
public class MainActivity extends Activity {
EditText name,age,email;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.name);
age = (EditText) findViewById(R.id.age);
email = (EditText) findViewById(R.id.email);
b = (Button) findViewById(R.id.done);
b.setOnClickListener(new View.OnClickListener() {
InputStream is = null;
@Override
public void onClick(View v) {
String username = name.getText().toString();
String userage = age.getText().toString();
String useremail = email.getText().toString();
//setting the nameValuePair
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("username",username));
nameValuePairs.add(new BasicNameValuePair("userage",userage));
nameValuePairs.add(new BasicNameValuePair("useremail",useremail));
//
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://sql17.000webhost.com/phpMyAdmin/index.php?db=a6923033_hamsau&lang=en-utf-8&token=4abc43994f0264c44701d85da5b524ee&phpMyAdmin=qsZJnJbsiQaPRx-aDIDtwuxOx5f,new.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
is = entity.getContent();
}
catch (UnsupportedEncodingException e) {
Log.i("Hammas","UnsupportedEncodingException "+e);
} catch (ClientProtocolException e) {
Log.i("Hammas", "ClientProtocolException " + e);
} catch (IOException e) {
Log.i("Hammas", "IOException " + e);
}
Toast.makeText(getApplicationContext(),"Data is entered",Toast.LENGTH_SHORT).show();
}
});
}
}
這裏是phpscript
enter code here
<?php
$mysql_host = "mysql17.000webhost.com";
$mysql_database = "a6923033_hamsau";
$mysql_user = "a6923033_hamsau";
$mysql_password = "123master95";
$name=$_POST['name'];
$age=$_POST['age'];
$email=$_POST['email'];
mysql_query("insert into users(name,age,email)values('{$name}','{$age}', {$email}')");
?>
我認爲有錯誤的HTTP鏈接或phpscript,因爲這些都只是一些我沒了解清楚
我當然希望那些不是你的實際DB憑證。如果他們是,你最好現在就去改變他們。 –
你能否更清楚你的答案? – Usman
你基本上只是把整個世界的鑰匙給了你的數據庫。你可能有一天醒來,發現一切已被刪除 – Osuwariboy