我拥有将我的应用程序链接到我的sql数据库的所有代码,在logcat中没有错误,应用程序没有崩溃,并且在toast消息“输入的数据”在所有catch子句的末尾成功显示但在表中数据库中没有输入任何数据。
这是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中存在错误,因为这些只是我不明白的事情
答案 0 :(得分:-1)
您的代码绝对正确且有效但有一点是您创建了具有相同名称和相同列名的表。