gpt4 book ai didi

PHP 不将 POST 数据插入数据库

转载 作者:行者123 更新时间:2023-11-29 05:18:21 25 4
gpt4 key购买 nike

我正在尝试将来自自动 POST 请求的数据插入到数据库中,但它没有插入,也没有抛出任何错误。 (错误日志中没有)代码:

<?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 读取一次。

From the manual

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));

关于PHP 不将 POST 数据插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29527888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com