简体   繁体   English

PHP未在SQL数据库插入语句中发布

[英]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. 因此,我尝试使用POST方法从HTMl中的表单收集数据,并使用MySQLi函数将所述数据插入MySQL数据库。 When in standalone PHP (without the database connection), I'm able to POST and echo data successfully to the second document. 在独立的PHP中(没有数据库连接),我可以POST并将数据成功回显到第二个文档。

However, once I open the connection to the database and attempt to use an insert statement, the data stops POSTing all together. 但是,一旦我打开与数据库的连接并尝试使用插入语句,数据就会停止一起进行POST。 I've tested the insert statement using strings, it works just fine. 我已经使用字符串测试了insert语句,它工作得很好。

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). 这是我的PHP(欢迎使用,如上述操作所示)。

$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. 总是返回New record created successfully的新记录,并将新记录插入数据库,但是所有字段均为空白。 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. 因此,显然不再获取我的POST数据。 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. 由于配置了/index.php/托管提供程序的方式,实际上我需要在操作中的文件夹名称后指定/index.php/ It was actually confusing the server and for some reason translated to 'GET' instead of 'POST'. 它实际上使服务器感到困惑,并出于某种原因将其转换为“ GET”而不是“ POST”。 Hope this helps. 希望这可以帮助。

Thanks for all the support! 感谢所有的支持!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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