PHP development post comment function tutorial database construction
We need to submit our opinions to the database and save them, and then submit them to the page in real time
Now we create a database named 'comments'
The code is as follows
<?php //建立'comments'数据库 header("Content-type:text/html;charset=utf-8"); //设置编码 $servername = "localhost"; $username = "root"; $password = "root"; // 创建连接 $conn = mysqli_connect($servername, $username, $password); mysqli_set_charset($conn,'utf8'); //设定字符集 // 检测连接 if (!$conn) { die("连接失败: " . mysqli_connect_error()); } // 创建数据库 $sql = "CREATE DATABASE comments"; if (mysqli_query($conn, $sql)) { echo "数据库创建成功"; } else { echo "数据库创建失败: " . mysqli_error($conn); } mysqli_close($conn); ?>
We are creating a data table named 'comments'
Field name | id | user | comment | addtime |
Field type | int | varchar | varchar | datetime |
Field Length | 11 | 30 | 200 | |
id | The commenter’s nickname | Comment content | Comment time |
<?php header("Content-type:text/html;charset=utf-8"); //设置编码 $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "comments"; // 创建连接 $conn = mysqli_connect($servername, $username, $password, $dbname); mysqli_set_charset($conn,'utf8'); //设定字符集 // 检测连接 if (!$conn) { die("连接失败: " . mysqli_connect_error()); } // 使用 sql 创建数据表 $sql = "CREATE TABLE `comments` ( `id` int(11) NOT NULL auto_increment, `user` varchar(30) NOT NULL, `comment` varchar(200) NOT NULL, `addtime` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM; " ; if (mysqli_query($conn, $sql)) { echo "数据表 comments 创建成功"; } else { echo "创建数据表错误: " . mysqli_error($conn); } mysqli_close($conn); ?>Create our main page index.html file