1

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?

10
  • Does the query fail? Check the return value of mysqli_query. Commented Apr 9, 2015 at 0:54
  • @Halcyon The request is sent by an automated script, so I cant read what the page says Commented Apr 9, 2015 at 0:57
  • You only have 3 parameters for your connection. mysqli_ php.net/manual/en/function.mysqli-connect.php requires 4; are you not choosing a database? Once you've done that, do mysqli_query($conn, $sql) or die(mysqli_error($conn)); Commented Apr 9, 2015 at 1:12
  • Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything also. Commented Apr 9, 2015 at 1:16
  • Plus, seeing ipaddr=192.168.0.4&id=8&endipaddr=***.**.230.62&id=8&end suggests a GET method, yet you're using two POST arrays. Your question is unclear. Post your HTML form. Commented Apr 9, 2015 at 1:25

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

It is able to write many requests into the text file from php://, and I dont even use that for actually getting the variables
I tried that code, and it has the same effect. Writes to outputfile.txt but doesnt put it in the database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.