【问题标题】:Inserting Form Data into MySQL, submit button redirects to php page and no data into SQL将表单数据插入 MySQL,提交按钮重定向到 php 页面,没有数据进入 SQL
【发布时间】:2017-06-01 17:16:21
【问题描述】:

我必须将数据注入到我的 SQL 表中,但我遇到了一个问题,当我填写表单并按下提交按钮时,页面会将我重定向到我的 php 文件,而且表是空的,因此没有数据注入,这是我的代码:

【问题讨论】:

  • 感谢格式化。
  • 1) 不要使用代码图片,将实际代码作为文本发布。 2)不要把它放在有链接的地方,把它放在你的问题中。
  • 一般 PHP 错误报告可能会有所帮助,请将 error_reporting(E_ALL); ini_set('display_errors', 1); 放在文件顶部,紧跟在 <?php 之后。虽然,为什么你的$sql 变量后面有{} 而不是;..?
  • @kevin 请编辑您的帖子并使用如下格式按钮将您的代码添加为文本{}
  • Please, don't use mysql_* functions in new code。它们不再维护and are officially deprecated。看到red box?了解prepared statements,并使用PDOMySQLi - this article 可以帮助您决定哪个。

标签: php jquery html mysql forms


【解决方案1】:

First things First mysql_* 函数已弃用,并且在最新的 php 版本中不再支持,您应该让 php Manuel 成为您最好的朋友,请使用带有准备好的语句的 mysqli 或 PDO。 p>

如果你还在学习 php,最好开始使用准备好的语句...你可以使用 mysqli Prepared 或 PDO。

选项 1 Mysqli

你的html:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>


    <form method="POST" action="insertform.php">

        Topic : <input type="text" name="topic"><br>
        Name   : <input type="text" name="name"><br>
        Attendance : <input type="text" name="attendance"><br>

        <button type="submit" name="submit">Submit</button>


    </form>

</body>
</html>

然后你的 insertform.php

<?php
$servername = "localhost";
$username   = "root";
$password   = "";
$dbname     = "kevintesting";

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

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}


if (isset($_POST['submit'])) {


    $topic      = $_POST['topic'];
    $name       = $_POST['name'];
    $attendance = $_POST['attendance'];

    // prepare and bind
    $stmt->$conn->prepare("INSERT INTO testformulario (Topic,Name,Attendance) VALUES (?,?,?)");
    $stmt->bind_param("sss", $topic, $name, $attendance);

    if ($stmt->execute()) {

        echo "New records created successfully";
    } else {

        echo "No insert";
    }

    $stmt->close();
    $conn->close();
}
?>

准备好的语句让你很容易。

希望这会有所帮助。

编辑:

上面查询中的问号 (?) 是占位符,我们使用它来防止 sql 注入。

bind_param() 函数 这是 SQL 查询的参数,并告诉数据库参数是什么。 “sss”参数列出了参数的数据类型。 s 字符告诉 mysql 参数是一个字符串。告诉mysql期望什么类型的数据,我们将SQL注入的风险降到最低。

注意:当您从外部来源插入任何数据时(如用户输入 从您的情况下的表格中),数据是非常重要的 消毒和验证。始终将用户输入视为来自非常 危险的黑客

带有 PDO 的选项 2

<?php
$servername = "localhost";
$username   = "root";
$password   = "";
$dbname     = "kevintesting";


try {

    $dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


}
catch (PDOException $e) {

    error_log("Could not connect" . $e->getMessage());

}



if (isset($_POST['submit'])) {


    $topic      = $_POST['topic'];
    $name       = $_POST['name'];
    $attendance = $_POST['attendance'];

    // prepare and bind

    try {

        $stmt = $dbh->prepare("INSERT INTO testformulario (Topic,Name,Attendance) VALUES(?,?,?)");

        if ($stmt->execute(array(
            $topic,
            $name,
            $attendance
        ))) {

            echo "Success";
        } else {

            echo "Fail "; // then check your error log
        }

    }
    catch (PDOException $e) {

        error_log($e->getMessage());

    }

}

?>

【讨论】:

    【解决方案2】:

    这是我的答案,它正在工作,类似于您对项目使用不同方法从一个页面获取表单数据并通过 php 页面存储数据的方法,如下所示。它可能不是干净的编码,但希望它对您有所帮助。希望对您有所帮助!

    <?php
    // Credentials
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "databasename";
    
    // Connect to Server
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check Connection
    if($conn->connect_error)
    {
        die('Could not Connect to Server: ' . $conn->connect_error);
    }
    
    // insert your forms submit name in replacement of form_submitname
    if(isset($_POST['form_submitname'])) 
    {
        $Topic = ($_POST['Topic']);
        $Name = ($_POST['Name']);
        $Attendance = ($_POST['Attendance']);
    
    // Insert your database Table name in replacement of Table_Name
    $sql= "INSERT INTO Table_Name(Topic, Name, Attendance)
    VALUES ('$Topic', '$Name', '$Attendance')";
    
    // echos back your records and values to display on PHP screen
    if ($conn->query($sql) === TRUE)
    {
        echo "<h1> New Record Created</h1>";
        echo "<br><br> Topic= " . $Topic;
        echo "<br> Name= " . $Name;
        echo "<br> Attendance= " . $Attendance;
    
    }else{
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
    $conn->close();
    }
    ?>
    

    这是我在我的数据库插入项目中尝试过并正在工作的东西,但绝不是完整的,或者是最好的方法,但如果您想尝试它,这只是一个开始,也许其他人可以根据需要进行改进.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-01
      • 2020-04-20
      • 2015-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多