我想写入位于我服务器上的数据库。Select-Statements 工作得很好,但我想在表中创建一个新条目。我为此使用 INSERT 并这样做:
Java 代码(应用程序/客户端):
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", idNumber.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("name", nameText.getText().toString()));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://olli130.myds.me/sqlcreate.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.i("Sent to Server", "name: " + nameText.getText().toString() + ", id: " + idNumber.getText().toString());
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
和 PHP 代码:
<?php
include 'sqlcon.php';
$connection=mysql_connect($mysqlhost, $mysqluser, $mysqlpwd) or die
("Verbindungsversuch fehlgeschlagen");
mysql_select_db($mysqldb, $connection) or die("Konnte die Datenbank nicht
waehlen.");
$sql = "INSERT INTO Testtable (id, descr)
VALUES (".$_REQUEST['id']", '".$_REQUEST['name']"')";
mysql_query($sql) or die (mysql_error());
mysql_close($connection);
?>
我做错了什么吗?为什么它不起作用?