我是 Android 编程的新手,我正在尝试获取用户输入并将其添加到我的外部数据库中。现在我的 php 代码是:
<?php
mysql_connect("host","login","pass") or die ("Unable to connect to MySQL");
mysql_select_db("database");
mysql_query("INSERT INTO Table (number, format, name, price) VALUES ('".$_REQUEST['number1']."', ".$_REQUEST['format1'].", ".$_REQUEST['name1'].", ".$_REQUEST['price1'].")");
mysql_close();
?>
我没有发现我的 php 有任何问题,但是当我运行我的程序时,我没有收到任何错误,但是当我查看我的数据库时,似乎实际上没有添加任何东西。我的 Java 代码是:
Log.d("debug", "ENTER METHOD ADD");
new Thread(new Runnable(){
public void run(){
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
EditText eNumber = (EditText) findViewById(R.id.barcodeEdit);
EditText eFormat = (EditText) findViewById(R.id.codeFormatEdit);
EditText eName = (EditText) findViewById(R.id.titleEdit);
EditText ePrice = (EditText) findViewById(R.id.priceEdit);
Log.d("debugNumber", eNumber.getText().toString());
Log.d("debugFormat", eFormat.getText().toString());
Log.d("debugName", eName.getText().toString());
Log.d("debugPrice", ePrice.getText().toString());
nameValuePairs.add(new BasicNameValuePair("number1", eNumber.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("format1", eFormat.getText().toString())); //you can add as many you need
nameValuePairs.add(new BasicNameValuePair("name1", eName.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("price1", ePrice.getText().toString()));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://MY_SITE.com/my.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
//is = entity.getContent();
Log.d("debug", "ENDED TRY");
}catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", e.toString());
//return false;
//Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}
}).start();
这是我添加的代码。我知道我可以很好地连接到数据库,因为我用来搜索数据库的代码工作正常,但由于某种原因我的添加搞砸了。自从前几天我了解到您不能在程序的主线程中执行这些 http 请求后,我就在新线程中运行代码。我一直在寻找答案,但还没有找到答案,也许我只是漏掉了一行,非常感谢任何帮助。
最佳答案
我建议您首先开发一个简单的 html 表单,以确保您在 php 方面不会遇到任何问题。
<html xmlns='http://www.w3.org/1999/xhtml'>
<head >
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
<title >Form Page: sampleform</title>
</head>
<body>
<h1>Sample form page</h1>
<form id='sampleform' method='post' action='http://MY_SITE.com/my.php' >
<p>
number1: <input type='text' name='number1' />
</p>
<p>
format1: <input type='text' name='format1' />
</p>
<p>
name1: <input type='text' name='name1' />
</p>
<p>
price1: <input type='text' name='price1' />
</p>
<p>
<input type='submit' name='Submit' value='Submit' />
</p>
</form>
</body>
</html>
运行此快速测试并告诉我。以便我可以进一步帮助您。
编辑:
是的。问题可能出在您的插入语句中。尝试使用以下代码。
<?php
$mysqli = new mysqli("Your host", "Db username", "Db password", "Db name");
if ($mysqli->connect_errno) {
echo "Couldn't connect to database";
}
$number = $mysqli->real_escape_string($_POST['number1']);
$format= $mysqli->real_escape_string($_POST['format1']);
$name= $mysqli->real_escape_string($_POST['name1']);
$price= $mysqli->real_escape_string($_POST['price1']);
$query = "INSERT INTO Table (number, format, name, price) VALUES('$number','$format','$name', '$price');";
$result = $mysqli->query($query);
if($result)
{
echo "Successfully inserted";
}
?>
关于java - 在 Android 中添加到外部数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15582935/