我正在尝试运行一个带有3个参数的php脚本。
这是我的简单脚本:
<?php
mysql_connect("localhost","login","pass");
mysql_select_db("android");
mysql_query("INSERT INTO people (name, sex, birthyear) VALUES ('".$_REQUEST['name']."', ".$_REQUEST['sex'].", ".$_REQUEST['year'].")");
mysql_close();
?>
我做错了什么,程序没有返回任何错误。 这是相同的程序:
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mysite.com/addPeople.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("name", "Maciek"));
nameValuePairs.add(new BasicNameValuePair("sex", "1"));
nameValuePairs.add(new BasicNameValuePair("year", "1981"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
答案 0 :(得分:0)
您绝对应该转义表单数据并在将其传递到数据库之前对其进行过滤。这可能会影响安全性。
您可以escape the variables with this function然后处理查询。
还要确保尝试访问数据库的用户具有适当的权限。
答案 1 :(得分:0)
我看到的唯一问题是你的mysql调用未指定连接因此将尝试进行未指定的连接。尝试将该行编辑为:
$conn = mysql_connect("localhost","login","pass");
mysql_select_db("android", $conn);
mysql_query("INSERT INTO people (name, sex, birthyear) VALUES ('".$_REQUEST['name']."', ".$_REQUEST['sex'].", ".$_REQUEST['year'].")", $conn);
我们还发现将mySQL查询函数包装在辅助函数中是有帮助的:
function query($q) {
global $conn;
$result = mysql_query($q, $conn);
if (!$result) {
die("<b>SQL Error:</b><br>---<b>QUERY:</b> $q<br><br><b>ERROR:</b> " . mysql_error());
}
return $result;
}
这允许我们处理错误,我们还记录查询,包括执行时间(不显示)。