php真的挺好玩的!
先写出前台页面index.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
label{
width: 60px;
display: inline-block;
text-align: right;
margin: 0 10px 0 0;
}
@media screen and (min-width: 360px) and (max-width: 767px) {
.content-div{
width: 90%;
margin: 5% auto;
}
.form-ele{
width: 50%;
margin: auto;
}
.content-div h2.head-title{
text-align: center;
}
.form-ele p input[type="text"]{
width: 80%;
}
.f-messages{
width: 80%;
resize: none;
height: 100px;
padding: 10px;
box-sizing: border-box;
}
.submit-btn{
width: 90px;
height: 35px;
background: rgba(0,233,2,0.4);
border: none;
color: block;
font-size: 1.2em;
cursor: pointer;
-webkit-transition:background 0.5s;
-ms-transition:background 0.5s;
-o-transition:background 0.5s;
transition:background 0.5s;
}
.submit-btn:hover{
background: rgba(0,233,2,0.7);
}
}
@media screen and (min-width: 768px) and (max-width: 1199px){
.content-div{
width: 90%;
margin: 5% auto;
}
.form-ele{
width: 50%;
margin: auto;
}
.content-div h2.head-title{
text-align: center;
}
.form-ele p input[type="text"]{
width: 70%;
}
.f-messages{
width: 80%;
resize: none;
height: 100px;
padding: 10px;
box-sizing: border-box;
}
.submit-btn{
width: 90px;
height: 35px;
background: rgba(0,233,2,0.4);
border: none;
color: block;
font-size: 1.2em;
cursor: pointer;
-webkit-transition:background 0.5s;
-ms-transition:background 0.5s;
-o-transition:background 0.5s;
transition:background 0.5s;
}
.submit-btn:hover{
background: rgba(0,233,2,0.7);
}
}
@media screen and (min-width: 1200px) and (max-width: 1365px) {
.content-div{
width: 90%;
margin: 5% auto;
}
.form-ele{
width: 50%;
margin: auto;
}
.content-div h2.head-title{
text-align: center;
}
.form-ele p input[type="text"]{
width: 80%;
}
.f-messages{
width: 80%;
resize: none;
height: 100px;
padding: 10px;
box-sizing: border-box;
}
.submit-btn{
width: 90px;
height: 35px;
background: rgba(0,233,2,0.4);
border: none;
color: block;
font-size: 1.2em;
cursor: pointer;
-webkit-transition:background 0.5s;
-ms-transition:background 0.5s;
-o-transition:background 0.5s;
transition:background 0.5s;
}
.submit-btn:hover{
background: rgba(0,233,2,0.7);
}
}
@media screen and (min-width: 1366px) and (max-width: 1920px) {
.content-div{
width: 90%;
margin: 5% auto;
}
.form-ele{
width: 50%;
margin: auto;
}
.content-div h2.head-title{
text-align: center;
}
.form-ele p input[type="text"]{
width: 80%;
}
.f-messages{
width: 80%;
resize: none;
height: 100px;
padding: 10px;
box-sizing: border-box;
}
.submit-btn{
width: 90px;
height: 35px;
background: rgba(0,233,2,0.4);
border: none;
color: block;
font-size: 1.2em;
cursor: pointer;
-webkit-transition:background 0.5s;
-ms-transition:background 0.5s;
-o-transition:background 0.5s;
transition:background 0.5s;
}
.submit-btn:hover{
background: rgba(0,233,2,0.7);
}
}
</style>
</head>
<body> <div class="content-div">
<h2 class="head-title">留言</h2>
<form action="add.php" method="post" class="form-ele">
<p>
<label for="names">姓名:</label><input id="names" type="text" name="names">
</p>
<p>
<label for="qq">QQ:</label><input id="qq" type="text" name="qq">
</p>
<p>
<label for="email">email:</label><input id="email" type="text" name="email">
</p>
<p>
<label for="messages">留言:</label><textarea id="messages" name="messages" class="f-messages"></textarea>
</p>
<p style="text-align: center;">
<input type="submit" name="sub" value="提交" class="submit-btn">
</p>
</form>
</div> </body>
</html>
效果:
我们要获取的是页面表单POST上来的内容,表单action的目标是add.php,add.php与index.php放在同一目录下(譬如:d:wamp/www/phpfile,服务器用wamp就可以测试)。
下面是add.php的代码:
<?php if($_POST['names']){ $host="localhost:3306";//本地服务器主机地址
$user = "root";//用户名
$password = "";//密码
$dbname = "phpbook";//数据库名字
$id = mysqli_connect($host,$user,$password,$dbname);//连接数据库,并将结果存在$id中
mysqli_query($id,"set names utf-8");//设置编码
mysqli_select_db($id,$dbname);//选择数据库
$names = $_POST["names"];//获取index.php表单中<input type="text" name="names">的内容
$qq = $_POST["qq"];//同上
$email = $_POST["email"];
$messages = $_POST["messages"];
$sql = "insert into users(names,qq,email,messages) values('$names','$qq','$email','$messages')";//sql语句,将获取的内容插入数据库
mysqli_query($id,$sql);//执行数据库语句 if($result = mysqli_query($id,"select * from users;")){//查询数据库中users中所有内容,并将结果存在$result中
echo "<table border='0'>
<tr>
<th>姓名</th>
<th>qq</th>
<th>email</th>
<th>留言内容</th>
</tr>";
while($row = mysqli_fetch_array($result)){//获取内容存在数组中
echo " <tr>
<td>".$row['names']."
</td>
<td>".$row['qq']."
</td>
<td>".$row['email']."
</td>
<td>".$row['messages']."
</td>
</tr>
";
}
echo "</table>";
}else{
echo "添加失败";
}
} ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>显示留言</title>
<style>
table{
border:none;
border-spacing:0;
width: 90%;
margin: auto;
}
</style>
</head>
<body> </body>
</html>
效果: