0

Note: This issue appears to be related to running MAMP/XAMPP on a Macbook Pro running El Capitan. The same DB/source files work perfectly on a Windows machine.

Note 2: THIS IS NOT A PRODUCTION APP. PLEASE DO NOT CRITIQUE THE CODE, YES IT IS VULNERABLE.

Note 3: Raw POST data:

file_get_contents("php://input"); = author=1&title=1&category=1&year=1&isbn=1



print_r($_POST); = Array()

Source: Learning PHP, MySQL, and Javascript 4th Edition. This is example 10-6. When I submit the form to add an additional row, nothing is actually posted to the DB.

I had the exact same issue with a query runner in another example from a different book. $_POST array is empty, but the raw post data shows a string w/ the correct information. $_GET does work. This seems to be limited to MAMP/XAMPP on this computer. I haven't been able to figure this out for the life of me. Most people with similar issues suggested verifying that the max post size variable in the php.ini file was set too low. The default is 128M which should be completely overkill for this script.

Does anyone have any suggestions, it seems to be config related.

<?php // sqltest.php
/**
 * Created by PhpStorm.
 * User: kg3
 * Date: 12/27/15
 * Time: 12:34 AM
 */

require_once 'connect.php';

if (isset($_POST['delete']) && isset($_POST['isbn']))
{
    $isbn   = get_post($conn, 'isbn');
    $query  = "DELETE FROM classics WHERE isbn='$isbn'";
    $result = $conn->query($query);
    if (!$result) echo "DELETE failed: $query<br>" .
        $conn->error . "<br><br>";
}

if (isset($_POST['author'])   &&
    isset($_POST['title'])    &&
    isset($_POST['category']) &&
    isset($_POST['year'])     &&
    isset($_POST['isbn']))
{
    $author   = get_post($conn, 'author');
    $title    = get_post($conn, 'title');
    $category = get_post($conn, 'category');
    $year     = get_post($conn, 'year');
    $isbn     = get_post($conn, 'isbn');
    $query    = "INSERT INTO classics VALUES" .
        "('$author', '$title', '$category', '$year', '$isbn')";
    $result   = $conn->query($query);

    if (!$result) echo "INSERT failed: $query<br>" .
        $conn->error . "<br><br>";
}

echo <<<_END
  <form action="sqltest.php" method="post"><pre>
    Author <input type="text" name="author">
     Title <input type="text" name="title">
  Category <input type="text" name="category">
      Year <input type="text" name="year">
      ISBN <input type="text" name="isbn">
           <input type="submit" value="ADD RECORD">
  </pre></form>
_END;

$query  = "SELECT * FROM classics";
$result = $conn->query($query);
if (!$result) die ("Database access failed: " . $conn->error);

$rows = $result->num_rows;

for ($j = 0 ; $j < $rows ; ++$j)
{
    $result->data_seek($j);
    $row = $result->fetch_array(MYSQLI_NUM);

    echo <<<_END
  <pre>
    Author $row[0]
     Title $row[1]
  Category $row[2]
      Year $row[3]
      ISBN $row[4]
  </pre>
  <form action="sqltest.php" method="post">
  <input type="hidden" name="delete" value="yes">
  <input type="hidden" name="isbn" value="$row[4]">
  <input type="submit" value="DELETE RECORD"></form>
_END;
}

$result->close();
$conn->close();

function get_post($conn, $var)
{
    return $conn->real_escape_string($_POST[$var]);
}
devpro
  • 16,184
  • 3
  • 27
  • 38
  • Can you show us the page with the form and also try `print_r($_POST);` at the top of your PHP page? – Matt Dec 27 '15 at 06:36
  • Unfortunately I cannot, I don't have a web server available to upload this to. I suspect that the issue is specific to running a local MAMP stack and that there would not be an issue on a live production server (Just a gut feeling.) As for print_r($_POST); All I get back is an empty array Array ( ) – Kevin Guilbault Dec 27 '15 at 06:57
  • Oh, sorry, the scroll bar didn't show up on the page. I'll take a look at the form. – Matt Dec 27 '15 at 07:03
  • The plot thickens. I loaded MAMP onto another PC I have (Windows 10), and moved the DB/source files over. It works without issue in that environment. The issue is limited to MAMP on a Macbook Pro running El Capitan. – Kevin Guilbault Dec 27 '15 at 07:29
  • **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Dec 27 '15 at 08:07
  • Quentin, I appreciate your input. This isn't a production application, it's an example (noted in the description.) Again, thanks for your concern, but it does not address the issue I am running into. – Kevin Guilbault Dec 27 '15 at 08:09
  • I can't help you with Mac / MAMP, but if you are serious about learning coding and in particular PHP, I can make a few suggestions which you can follow or ignore :) 1. Use vagrant and have simple ubuntu or centos server so you are running a real LAMP environment on your Mac or PC 2. If you can learn via video, try laracasts. There is great stuff on coding as well as laravel, the php framework 3. Learn to separate your code into classes, use templating to generate HTML, etc 4. visit phptherightway.com for lots of free and useful php goodness – brianlmerritt Dec 27 '15 at 08:21

1 Answers1

0

Figured it out.

It ended up not being Apache, PHP Version, my code, or anything else I'd mentioned previously. I use PHPStorm IDE and had set it up with MAMP a while back. The ports for that installation were different than my current installation and I didn't realize. Some of the network traffic was getting through that Port but $_POST was not. After correcting the port in the URL and updating PHPStorm everything is working now. 2 days to figure that out....DOH