【问题标题】:Inserting HTML Form data into MySQL with PHP使用 PHP 将 HTML 表单数据插入 MySQL
【发布时间】:2017-05-14 14:53:29
【问题描述】:

我正在尝试制作一个简单的留言板 MySQL 数据库,您可以在其中撰写评论并通过 HTML 表单在一个页面上提交,并在提交评论后在单独的页面上查看所有评论。

我的问题是 HTML 表单中的两个字段没有插入到我的 MySQL 数据库中,这导致我查看所有评论页面缺少名称和标题。

Link to what the "Read all Reviews" page looks like.

当我只用 PHP 进行 MySQL 查询测试时,代码可以正常工作,但我需要我的 HTML 表单才能工作。

HTML 表单:

<form action ="process.php" method = "post">
            <fieldset>
                <legend>Review Field</legend>
                Reviewer Name: <br />
                <input type="text" name "name" id = "name"><br />
                Title of Review:<br />
                <input type="text" name "title" id = "title"><br />
                Enter your review below:
                 <!--Textbox start-->   
                 <textarea name="body" id = "body" rows="10" cols="100">
                 </textarea>
                  <!--Textbox end-->    
                <br />
                <input type="submit" name = "submit" id="submit">
                <br />
            </fieldset>
            </form>

process.php 的代码:

<?php // Create a database connection.
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "password";
$dbname = "ya_reviews";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

//Test if connection occurred.
if (mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}

//Perform database query
$name = $_POST['name'];
$title = $_POST['title'];
$body = $_POST['body'];

//This function will clean the data and add slashes.
// Since I'm using the newer MySQL v. 5.7.14 I have to addslashes
$name = mysqli_real_escape_string($connection, $name);
$title = mysqli_real_escape_string($connection, $title);
$body = mysqli_real_escape_string($connection, $body);

//This should retrive HTML form data and insert into database
$query  = "INSERT INTO reviews (name, title, body) 
            VALUES ('".$_POST["name"]."','".$_POST["title"]."','".$_POST["body"]."')";

        $result = mysqli_query($connection, $query);
        //Test if there was a query error
        if ($result) {
            //SUCCESS
        header('Location: activity.php');
        } else {
            //FAILURE
            die("Database query failed. " . mysqli_error($connection)); 
            //last bit is for me, delete when done
        }

mysqli_close($connection);
?>

查看所有评论:

<?php

        //This will fetch the data from the database 
        $query = "SELECT * FROM reviews";
        $result = mysqli_query($connection, $query);
        //Test if there was a query error
        if (!$result) {
            die("Database query failed.");
        }

        // This will let me display the data.
        // The loop will be spilt so I can format with HTML
        while ($row = mysqli_fetch_assoc($result)) {
            //output data from each row
        ?>

        Name: <?php echo $row["name"] . "<br />"; ?>
        Title: <?php echo $row["title"] . "<br />"; ?>
        Review: <?php echo $row["body"] . "<br />";
            echo "<hr>"; ?>
        <?php
        } ?>

注意:我用上面代码之前在 process.php 中看到的相同代码连接到数据库,我排除了它以节省空间。

【问题讨论】:

  • 请使用准备好的语句和mysqli-并且不要忘记更改您的密码

标签: php mysql post forms http-post


【解决方案1】:

哟,你只是遗漏了一些语法,因此在从这些元素中收集数据时会产生错误,

<input type="text" name "title" id = "title">

您缺少名称参数中的“=”符号

【讨论】:

    【解决方案2】:

    问题是 HTML 中的 name 属性不正确。

    <input type="text" name="name" id = "name"><br />
    
    <input type="text" name="title" id = "title"><br />
    

    【讨论】:

      【解决方案3】:

      我认为你搞砸了 HTML 的语法

      <form action ="process.php" method = "post">
                  <fieldset>
                      <legend>Review Field</legend>
                      Reviewer Name: <br />
                      <input type="text" name="name" id = "name"><br />
                      Title of Review:<br />
                      <input type="text" name="title" id = "title"><br />
                      Enter your review below:
                       <!--Textbox start-->   
                       <textarea name="body" id = "body" rows="10" cols="100">
                       </textarea>
                        <!--Textbox end-->    
                      <br />
                      <input type="submit" name = "submit" id="submit">
                      <br />
                  </fieldset>
                  </form>
      

      一定会成功的!

      【讨论】:

        【解决方案4】:

        您的 HTML 属性语法不正确。它在attributevalue 之间缺少= 符号。

        name "name" 更改为name="name"name "title" 更改为name="title"

        <input type="text" name="name" id = "name"><br />
                    Title of Review:<br />
        <input type="text" name="title" id = "title"><br />
        

        insert 期间,您也没有使用转义值。

        在插入查询中使用$name 而不是$_POST["name"]。标题和正文值也是如此。

        【讨论】:

        • 非常感谢! ^^ 语法错误和混淆我的变量似乎是我的死敌。 >
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-02-26
        • 1970-01-01
        • 2013-10-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-08
        • 2017-10-20
        相关资源
        最近更新 更多