使用HTML表单和MySQL数据库在PHP中的注释部分

时间:2015-10-01 08:45:54

标签: php html mysql include comments

我非常非常 PHP-并且正在尝试通过实践学习自学一些网络编程。我偶然发现了这个:https://www.youtube.com/watch?v=CS45YAqCgX8关于如何构建评论部分的视频系列,但我被卡住了。

我的第一个PHP文件看起来像这样:

<?php
require('connect.php');
If(isset($_POST['submit'])) 
{ 
    $name=$_POST['name']; 
    $comment=$_POST['comment']; 

If(name&&comment) 
{
    $insert=mysqli_query("INSERT INTO comment (name,comment) VALUES ('$name','$comment')");
} 
}       
/*else
echo "Please fill out your name and leave a comment.*/



?>
<html>
<br/>
<br/>
<body>
<form action = "CommentForm.php" method = "post">
<table>
<br/>
<tr><td>Name: </td><td><input type = "text" name = "name" size = "30"/></td></tr>
<tr><td colspan="2">Comment:</td></tr>
<tr><td colspan="2"><textarea name = "comment"></textarea></td></tr>
<tr><td colspan="2"><input type = "submit" name = "submit" value = "Comment"/></td></tr>
</table>
</form>
</body>
</html>

连接文件为:

<?php
    mysqli_connect("My Host", "My Username","My Password");
    mysqli_select_db("My Database");
?>

- 我想要附加的HTML就是这样

<!DOCTYPE html>
   <html>
      <head>
         <meta charset = "utf-8">
        <title>Pokemon</title>
        <link rel = "stylesheet" type = "text/css" 
           href = "stylishandviewtiful.css">
      </head>
      <body>
         <br/>
         <br/>
         <br/>
         <br/>
        <h3><a href = "Competitive Meta and Community.html">Competitive Meta and Community</a></h3>
        <h3><a href = "Game StorylinesTheoriesOverthought.html">In-Game Story Ideas and Opinions</a></h3>
      </body>
      <?php include 'CommentForm.php'; ?>
    </html>

我想我不能只使用包含功能?我是否会看到更好的回报将第一个PHP应用程序中的HTML分离到自己的文件中?

我可能通常需要一个针对此特定实例的教程,该教程的启动速度足以让任何门外汉跟随。

1 个答案:

答案 0 :(得分:0)

你忘了把连接放在mysqli_query里面($ con,“insert ...”) 否则你的插入将链接到$ link。第二次尝试连接这样的$ con = mysqli_connect(“localhost”,“my_user”,“my_password”,“my_db”);将你的$ con传递给你的mysqli_query

请参阅http://www.w3schools.com/php/func_mysqli_query.asp

如果您的表单仍未显示,则需要查看您的包含文件。如果出现问题则会中止