简体   繁体   中英

PHP Not inserting POST data into database

I am trying to insert data from an automated POST request into a database, but it is not inserting it and is throwing no errors. (None in error log) Code:

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

?>

The content of outputfile.txt is this:

ipaddr=192.168.0.4&id=8&endipaddr=***.**.230.62&id=8&end

However no data is ever inserted into the database. Am I making a simple mistake that I am not noticing?

You can only read in from php://input once in PHP versions prior to 5.6.

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.

As a work around you can extract those values from 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'];

Another thing is, you only have 3 parameters for your connection. mysqli_ requires 4.

Once you've done that, do mysqli_query($conn, $sql) or die(mysqli_error($conn));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM