用PHP将HTML表单数据插入MySQL

用PHP将HTML表单数据插入MySQL,php,mysql,post,forms,http-post,Php,Mysql,Post,Forms,Http Post,我正在尝试创建一个简单的留言板MySQL数据库,在这个数据库中,您可以在一个页面上编写评论并通过HTML表单提交,并且在提交评论后在另一个页面上查看所有评论 我的问题是HTML表单中的两个字段没有插入MySQL数据库,这导致我的view all reviews页面缺少名称和标题 当我用PHP进行MySQL查询时,代码可以正常工作,但我需要HTML表单才能工作 HTML表单: <form action ="process.php" method = "post">

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

我的问题是HTML表单中的两个字段没有插入MySQL数据库,这导致我的view all reviews页面缺少名称和标题

当我用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>
<?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的代码:

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

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

name“name”
更改为
name=“name”
,将
name“title”更改为
name=“title”


审查标题:

另外,在
insert
过程中,您没有使用转义值


在插入查询中使用
$name
而不是
$\u POST[“name”]
。标题和正文值也是如此。

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

name“name”
更改为
name=“name”
,将
name“title”更改为
name=“title”


审查标题:

另外,在
insert
过程中,您没有使用转义值


在插入查询中使用
$name
而不是
$\u POST[“name”]
。标题和正文值也是一样。

我想你搞乱了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>

审查领域
审核人姓名:

审查标题:

在下面输入您的评论:


它肯定会成功的

我想你把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>

审查领域
审核人姓名:

审查标题:

在下面输入您的评论:


它肯定会成功的

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

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

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



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

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

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



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

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


您缺少name参数中的“=”符号,只是缺少一些语法,因此在从这些元素收集数据时会产生错误

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


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

请在mysqli中使用预置语句-别忘了更改密码请在mysqli中使用预置语句-别忘了更改密码谢谢语法错误和混淆变量似乎是我的死敌。>非常感谢!^^语法错误和混淆变量似乎是我的死敌。>