简体   繁体   English

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

[英]Inserting HTML Form data into MySQL with PHP

I'm trying to make a simple message board MySQL database where you can write a review and submit it via an HTML form on one page and view all of the reviews on a separate page once you've submitted your review. 我正在尝试制作一个简单的留言板MySQL数据库,您可以在其中编写评论并通过HTML表单在一页上提交评论,并在提交评论后在单独的页面上查看所有评论。

My problem is two of the fields from the HTML form are not being inserted into my MySQL database which results in my view all reviews page to be missing the Name and Title. 我的问题是HTML表单中的两个字段没有插入到我的MySQL数据库中,这导致我的“查看所有评论”页面缺少名称和标题。

Link to what the "Read all Reviews" page looks like. 链接到“阅读所有评论”页面的外观。

The code works without any issue when I tested it doing MySQL queries with just PHP but I need my HTML form to work. 当我仅用PHP对MySQL查询进行测试时,代码就可以正常工作,但是我需要HTML表单才能工作。

HTML form: 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>

Code for process.php: 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);
?>

View all Reviews: 查看所有评论:

<?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
        } ?>

Note: I connected to the database with the same code seen in process.php before the above code, I excluded it to save space. 注意:我使用上面代码之前在process.php中看到的相同代码连接到数据库,因此为了节省空间,我将其排除在外。

Your HTML attribute syntax is incorrect. 您的HTML属性语法不正确。 Its missing = sign between attribute and value . 它的缺失=之间符号attributevalue

Change name "name" to name="name" and name "title" to name="title" 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 />

Also during insert you aren't using escaped values. 同样在insert过程中,您不会使用转义值。

Use $name instead of $_POST["name"] in insert query. 在插入查询中使用$name代替$_POST["name"] Same goes for title and body values. 标题和正文值也一样。

The problem is that the name attribute is not correct in HTML. 问题在于name属性在HTML中不正确。

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

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

I think you messed up with syntax of 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>

It will work surely! 它一定会工作!

Yo, you're just missing some syntax, therefore creating errors when it comes to gathering the data from those elements, 哟,您只是缺少一些语法,因此从这些元素中收集数据时会产生错误,

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

You're missing the "=" sign from the name parameter 您缺少名称参数中的“ =”符号

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

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