1

当我尝试从 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();
         }
     }
     });


    }
4

0 回答 0