当我尝试从Android模拟器运行它时,我的代码插入一个空字段。
php脚本没问题,当我用实际值替换变量时,它可以工作。
当我自己运行脚本时,它会向我的数据库发送一行,该行为空白。
<?php
$connect = mysql_connect("localhost","","") or die(mysql_error());
mysql_select_db("tedd_fyp") or die(mysql_error());
$username = $_POST['username'];
$password = $_POST['password'];
$query="INSERT INTO name (username, password) VALUES ('$username' ,'$password')";
$res = mysql_query($query) or die ("Error: ". mysql_error(). " with query ");
mysql_close($connect);
echo $res;
?>
这是我的java代码,无法连接到我的php脚本。
当我运行此脚本时,没有任何反应。
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
Button registerLecturer=(Button)findViewById(R.id.button1);
registerLecturer.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String username = editText1.getText().toString();
String password = editText2.getText().toString();
try
{
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://tedd.5gbfree.com/insert.php");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}