PHP 未将 POST 数据插入数据库
PHP Not inserting POST data into database
我正在尝试将来自自动 POST 请求的数据插入数据库,但它没有插入,也没有抛出任何错误。 (None 在错误日志中)
代码:
<?php
function getBetween($content,$start,$end){
$r = explode($start, $content);
if(isset($r[1])) {$r = explode($end, $r[1]);
return $r[0]; } return''; }
file_put_contents("outputfile.txt", file_get_contents("php://input"), FILE_APPEND );
$cip = $_POST['ipaddr'];
$cid = $_POST['id'];
$conn = mysqli_connect('localhost', '********', '*******');
$sql = "INSERT INTO slso (asid, ips) VALUES ('$cid', '$cip')";
mysqli_query($conn, $sql); mysqli_close($conn);
?>
outputfile.txt的内容是这样的:
ipaddr=192.168.0.4&id=8&endipaddr=***.**.230.62&id=8&end
然而,没有数据被插入到数据库中。我犯了一个我没有注意到的简单错误吗?
在 5.6 之前的 PHP 版本中,您只能从 php://input
读入一次。
Prior to PHP 5.6, a stream opened with php://input could only be read once; the stream did not support seek operations. However, depending on the SAPI implementation, it may be possible to open another php://input stream and restart reading. This is only possible if the request body data has been saved. Typically, this is the case for POST requests, but not other request methods, such as PUT or PROPFIND.
作为解决方法,您可以从 php://input
:
中提取这些值
$post = file_get_contents("php://input");
file_put_contents("outputfile.txt", $post, FILE_APPEND );
parse_str($str, $output);
$cip = $output['ipaddr'];
$cid = $output['id'];
另一件事是,您的连接只有 3 个参数。 mysqli_
需要 4 个。
完成后,mysqli_query($conn, $sql) or die(mysqli_error($conn));
我正在尝试将来自自动 POST 请求的数据插入数据库,但它没有插入,也没有抛出任何错误。 (None 在错误日志中) 代码:
<?php
function getBetween($content,$start,$end){
$r = explode($start, $content);
if(isset($r[1])) {$r = explode($end, $r[1]);
return $r[0]; } return''; }
file_put_contents("outputfile.txt", file_get_contents("php://input"), FILE_APPEND );
$cip = $_POST['ipaddr'];
$cid = $_POST['id'];
$conn = mysqli_connect('localhost', '********', '*******');
$sql = "INSERT INTO slso (asid, ips) VALUES ('$cid', '$cip')";
mysqli_query($conn, $sql); mysqli_close($conn);
?>
outputfile.txt的内容是这样的:
ipaddr=192.168.0.4&id=8&endipaddr=***.**.230.62&id=8&end
然而,没有数据被插入到数据库中。我犯了一个我没有注意到的简单错误吗?
在 5.6 之前的 PHP 版本中,您只能从 php://input
读入一次。
Prior to PHP 5.6, a stream opened with php://input could only be read once; the stream did not support seek operations. However, depending on the SAPI implementation, it may be possible to open another php://input stream and restart reading. This is only possible if the request body data has been saved. Typically, this is the case for POST requests, but not other request methods, such as PUT or PROPFIND.
作为解决方法,您可以从 php://input
:
$post = file_get_contents("php://input");
file_put_contents("outputfile.txt", $post, FILE_APPEND );
parse_str($str, $output);
$cip = $output['ipaddr'];
$cid = $output['id'];
另一件事是,您的连接只有 3 个参数。 mysqli_
需要 4 个。
完成后,mysqli_query($conn, $sql) or die(mysqli_error($conn));