我是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();
&GT;
我没有看到我的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请求。我一直在寻找答案,但还没有找到答案,也许我只是错过了一条线,非常感谢任何帮助。
答案 0 :(得分:1)
我建议你先开发一个简单的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>
运行此快速测试并告诉我。所以我可以进一步帮助你。
修改强>
是。问题可能出在insert语句中。请尝试使用以下代码。
<?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";
}
?>
答案 1 :(得分:0)
显然它在代码中没有显示错误。要逐行调试,您应该修改PHP代码并尝试 -
<?php
mysql_connect("host","login","pass") or die ("Unable to connect to MySQL");
mysql_select_db("database");
echo $_REQUEST['number1'];
echo $_REQUEST['format1'];
echo $_REQUEST['name1'];
echo $_REQUEST['price1'];
$sql = "INSERT INTO Table (number, format, name, price) VALUES ('".$_REQUEST['number1']."', '".$_REQUEST['format1']."', '".$_REQUEST['name1']."', '".$_REQUEST['price1']."')";
echo $sql;
mysql_query($sql) or die(mysql_error());
mysql_close();
?>
您也可以尝试使用$ _POST语法而不是$ _REQUEST。
你应该理解这个错误。
请与我们分享结果,以便我们为您提供进一步的帮助。
由于