2016-12-30 69 views
0

我想要制作一个简单的留言板MySQL数据库,您可以在其中撰写评论并通过HTML表单在一个页面上提交,并在一个单独的页面上查看所有评论您已提交您的评论。用PHP插入HTML表单数据到MySQL

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

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

代码工作没有任何问题,当我测试了它做的MySQL查询只用PHP,但我需要我的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中的相同代码连接到了数据库,为了节省空间,我排除了它。

+0

请使用mysqli-准备好的语句,不要忘记更改密码 – Strawberry

回答

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

+0

非常感谢! ^^ 语法错误和混合我的变量似乎是我的致命的敌人。 >< – stressedgradstudent

0

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

的语法搞砸它一定会努力!

1

问题是名称属性在HTML中不正确。

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

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

哟,你只是缺少一些语法,因此产生错误,当谈到从这些元素收集数据,

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

你从名称缺少“=”号参数