繁体   English   中英

使用PHP将HTML表单数据插入MySQL

[英]Inserting HTML Form data into MySQL with PHP

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

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

链接到“阅读所有评论”页面的外观。

当我仅用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中看到的相同代码连接到数据库,因此为了节省空间,我将其排除在外。

您的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"] 标题和正文值也一样。

问题在于name属性在HTML中不正确。

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

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

我认为您搞砸了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>

它一定会工作!

哟,您只是缺少一些语法,因此从这些元素中收集数据时会产生错误,

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

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

暂无
暂无

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

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