在我的应用程序中,我有一个特定的表格供用户填写,我想将这些数据存储在我的在线数据库中。
这是我在java中的代码:
public void send_data_to_DB(){
String result = "";
InputStream is = null;
StringBuilder sb=null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();//() before
nameValuePairs.add(new BasicNameValuePair("table", table));
nameValuePairs.add(new BasicNameValuePair("code", Integer.toString(code)));
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("email", email));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://myurl.php");
HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs);
httppost.addHeader(entity.getContentType());
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
//HttpEntity entity = response.getEntity();
//is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
这是我的 php 脚本:
<?php
mysql_connect("dserver","User","Code");
mysql_select_db("DB_Name");
$table.=$_POST['table'];
$code.=$_POST['code'];
$name.=$_POST['name'];
$email.=$_POST['email'];
$q=mysql_query(" INSERT INTO {$table} (code,name,email) VALUES ({$code},{$name},{$email}) ")or die(mysql_error());
mysql_close();
?>
我认为这一定是我的 php 中的某些东西以及我分配或使用变量的方式,但我在 PHP 方面不太有经验。你能帮助我吗?