index.php
<?php
//获取数据库中的数据
try{
$con=new PDO("mysql:host=localhost;dbname=beixidb;charset=utf8;port=3306",'root','');
$con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$res=$con->query('select * from news');
$data=$res->fetchAll(PDO::FETCH_ASSOC);
// print_r($data);
//字符串的拼接
$str='';
for($i=0;$i<count($data);$i++){
$str.="<tr><td>{$data[$i]['id']}</td><td>{$data[$i]['title']}</td><td>{$data[$i]['author']}</td><td>{$data[$i]['des']}</td>
<td>{$data[$i]['content']}</td><td>{$data[$i]['time']}</td><td><a href='#'>删除</a></br><a href='#'>修改</a></td></tr>";
}
}catch(PDOException $e){
echo $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.container{
width:800px;
height:600px;
margin:20px auto;
border:1px solid #ccc;
overflow:hidden;
}
.left,.right{
float:left;
}
.left{
width:120px;
}
table{
width:620px;
}
.left a{
display:block;
text-decoration:none;
color:#000;
padding:10px 0;
border-bottom:1px solid #efefef;
}
</style>
</head>
<body>
<div class="container">
<div class="left">
<a href="">所有文档列表</a>
<a href="添加文档.html">添加列表</a>
</div>
<div class="right">
<table border="1" cellpadding="0" cellspacing="0" width="600">
<tr>
<th>ID</th>
<th>标题</th>
<th>作者</th>
<th>简介</th>
<th>时间</th>
<th>操作</th>
</tr>
<?php echo $str?>
</table>
</div>
</div>
</body>
</html>
添加文档.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.container{
width:800px;
margin:20px auto;
border:1px solid #ccc;
overflow:hidden;
}
.left,.right{
float:left;
}
.left{
width:120px;
}
input{
width: 500px;
}
.left a{
display:block;
text-decoration:none;
color:#000;
padding:10px 0;
border-bottom:1px solid #efefef;
}
textarea{
vertical-align: top;
margin-top: 5px;
width: 500px;
resize: none;
}
.btn{
display: block;
width: 80px;
height: 50px;
border: none;
background: orange;
border-radius: 5px;
color: #fff;
margin: 10px auto;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<div class="left">
<a href="#">添加文档页面</a>
</div>
<div class="right">
<form action="addnews.php" method="get">
标题:<input type="text" name="title"><br/>
作者:<input type="text" name="author"><br/>
简介:<input type="text" name="des"><br/>
内容:<textarea type="text" name="content" cols="30" rows="10"></textarea><br/>
<input type="submit" value="保存" class="btn">
</form>
</div>
</div>
</body>
</html>
addnews.php
<?php
echo '<pre>';
//接收用户传来的数据
$title=isset($_GET['.title'])?$_GET['title']:'';
$author=isset($_GET['.author'])?$_GET['author']:'';
$des=isset($_GET['.des'])?$_GET['des']:'';
$content=isset($_GET['.content'])?$_GET['content']:'';
//生成一个时间戳
$time=time();
//写入数据库
$con=new PDO("mysql:host=localhost;dbname=beixidb;charset=utf8;port=3306",'root','');
$res=$con->query("insert into news (title,author,des,content,time) values('{$title}','$author','$des','$content','$time')");
if($res){
echo '<script> alert("添加成功");window.location.href="index.php"</script>';
}
else{
echo '添加失败';
}
?>
老师,为什么这个案例添加成功后 标题 作者等信息不显示,只显示时间戳,是没添加进去吗?具体代码如上:
效果图附下:
