简体   繁体   中英

PHP Not Posting in SQL Database Insert Statement

So I'm attempting to gather data from a form in HTMl using the POST method and insert said data into a MySQL database using MySQLi functions. When in standalone PHP (without the database connection), I'm able to POST and echo data successfully to the second document.

However, once I open the connection to the database and attempt to use an insert statement, the data stops POSTing all together. I've tested the insert statement using strings, it works just fine.

Here's my form:

<form action="welcome" method="post">
    First Name*<br>
    <input type="text" name="userFN" required><br><br>
    Last Name*<br>
    <input type="text" name="userLN" required><br><br>
    Email*<br>
    <input type="email" name="userEmailAddress" required><br><br>
    Password*<br>
    <input type="password" name="userPassword" required><br><br>
    Tell Us About Yourself<br>
    <textarea rows="4" cols="50" name="userDescription">
    </textarea>
    <br>
    <input type="submit">
</form>

Here's my PHP (welcome, as used in the action above).

$conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection

    if ($conn->connect_error) {

        die("Connection failed: " . $conn->connect_error);

    } 

    //post form info into variables
    $userFN = mysqli_real_escape_string($conn, $_POST['userFN']);
    $userLN = mysqli_real_escape_string($conn, $_POST['userLN']);
    $userEmailAddress = mysqli_real_escape_string($conn, $_POST['userEmailAddress']);
    $userPassword = mysqli_real_escape_string($conn, $_POST['userPassword']);
    $userDescription = mysqli_real_escape_string($conn, $_POST['userDescription']);

    $sql = "INSERT INTO users (userFN, userLN, userEmailAddress, userPassword, userDescription) VALUES" . " ('" . $userFN .  "','" . $userLN . "','" . $userEmailAddress . "','" . $userPassword . "','" . $userDescription . "');";

    echo $sql;

    if ($conn->query($sql) === TRUE) {

        echo "New record created successfully";

    } else {

        echo "Error: " . $sql . "<br>" . $conn->error;

    }

It always returns New record created successfully , and inserts a new record into the database, but all of the fields are blank. When I echo the insert statement, I get this:

INSERT INTO users (userFN, userLN, userEmailAddress, userPassword, userDescription) VALUES ('','','','','');

So it's clearly not getting my POST data anymore. I've tried using prepared statements and still no luck. What am I doing wrong here? I greatly appreciate any and all advice, thank you in advance.

The answer may surprise you.

So the issue is actually in the form "action". Because of the way my webhosting provider is configured, I actually needed to specify /index.php/ after the folder name in the action. It was actually confusing the server and for some reason translated to 'GET' instead of 'POST'. Hope this helps.

Thanks for all the support!

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