<?php
$servername = "localhost";
$username = "user";
$password = "password";
$database = "mydatabase";
$mypassword = "mypassword";
$receivedPassword = $_POST["pwd"];
if ($receivedPassword != $mypassword) {
print "cedential failure";
} else {
$unfiltered = $_POST["query"];
// print("unfiltered $unfiltered");
$myquery = htmlspecialchars($unfiltered);
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// print("filtered: $myquery");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
print("conn ssuccessfull");
}
print('performing query: ' . $myquery);
//$quer = (string)$myquery;
$res = $conn->query($myquery); // or die(mysql_error());
$enc = mb_detect_encoding($myquery);
print("<br> encoding is: $enc <br>");
$rows = array();
while ($r = mysqli_fetch_assoc($res)) {
$rows[] = $r;
print($r);
}
print("ehere");
print json_encode($rows);
$conn->close();
}
?>
下面是我打印回给我的一些示例。php _POST查询参数失败,但硬编码文本查询成功
康恩全成
执行查询:SELECT * FROM music_log2 WHERE(central_time> '2016年2月3日2时00分00秒' AND central_time < '2016年2月3日四点零零分00秒')
编码:ASCII
ehere []
在服务器日志看,我看到:
2016年2月27日上午04时41分26秒 - WWW:PHP的警告:mysqli_ FETCH_ASSOC()预计参数1被mysqli_result,在/path/to/my/file.php定的boolean在线43
注意,在我的示例输出,我没有得到的结果。事实上,服务器日志表明$水库是假的后:
res = $conn->query($myquery); // or die(mysql_error());
我已经试过了模具的选择,你可以看到注释掉了,但我没有得到任何错误输出,只是没有更多的照片。
当我把通过更改查询行硬编码准确的查询:
$res = $conn->query("SELECT * FROM music_log2 WHERE (central_time > '2016-02-03 2:00:00' AND central_time < '2016-02-03 4:00:00')");
它工作正常。 下面是从改变上述行一些输出:
康恩全成
执行查询:SELECT * FROM music_log2 WHERE(central_time> '2016年2月3日2时00分00秒' AND central_time <“2016-02 -03 4点00' 分○○秒)
编码是:ASCII
ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray [{ “field_number”: “6”, “值”: “一切 以外旋转”, “central_time”:“2016年2月3日 03:59:15“,”user_agent“:”API“,”id“:”1084793“},{”field_number“:”2“,”value“:”标题 Fig ht“,”central_time“:”2016-02-03 03:59:15“,”user_agent“:”API“,”id“:”1084794“},{”field_number“:”3“,”value“ :“Hyperview”,“central_time”:“2016-02-03 03:59:15”,“user_agent”:“API”,“id”:“1084795”},{“field_number”:“4”值 “:” 氯”, “central_time”: “2016年2月3日 3时59分15秒”, “USER_AGENT”: “API”, “ID”: “1084796”},
...
帮助。我非常难过。现在在这工作了2天。
我也试着包裹查询在双引号:
$res = $conn->query('"' . $myquery . '"');
同空的结果。
使用'mysqli_real_escape_string'替换'htmlspecialchars'。 –
如果整个查询由用户输入提供,那么没有函数会为您提供SQL注入的任何保护。 – Devon
@ A.B。在这个问题的背景下,这会是什么推理呢?转义字符串函数在这里不起作用,他不是逃避单个值,而是整个查询。 – Devon