前言
紧张的赛前培训迎来了一次休息天,然后看了看自己落下的课程作业,唉,有个课程设计,大概看了一下是用PHP+MYSQL+HTML来写,感觉应该还能写,就利用这一天来写一下吧,先上个最后的效果图。
针对文章所提及公司,若不支持发表,请联系本人进行下架文章
QQ:515469508
网站构造
老师提的要求是能够实现管理用户的登录,能发表新闻,发表后能在前端页面加载出来。
网站构造
image //存放图片
index.php //网站首页
login.php //登录页面
logincheck.php //登录检查页面
news.php //查看文章页面
manger.php //发表文章页面
MYSQL连接
当然,在进行连接之前,准备工作也要做好,新建一个数据库,里面有两个表,一个users,一个news.
users表
news表
这里我是用的PDO进行连接的数据库,当然连接数据库的方法并不唯一,可以跟随个人喜好来选择
1 2 3 4 5 6 7 8
| function linkdatabase(){ try{ $pdo = new pdo("mysql:host=localhost:3306;dbname=mydata","root","******"); return $pdo; }catch(exception $e){ echo $e->getmessage(); } }
|
将连接数据库写成了一个linkdatabase()函数,里面写了一个异常处理,能成功连接返回$pdo,若不能成功连接则报错。
前端呈现
当我们在manager.php页面发表了文章之后,文章会写入数据库中,我们要让其自动在前端展示,例如:
代码实现:
1 2 3 4 5 6 7 8 9 10
| $pdo = linkdatabase(); $str = "select title,data from news"; $q = $pdo->query($str);
while($f = $q->fetch(PDO::FETCH_NUM)) { echo "<tr>"; echo "<td width=491 height=36><a href='news.php?new=".$f[0]."'>".$f[0]."</a></td>"; echo "<td width=491 height=36>".$f[1]."</td>"; }
|
我们在连接数据库成功后,开始执行select title,data from news
在news表中找title,data。
PDO::FETCH_NUM
返回一个索引为以0开始的结果集列号的数组
所以echo
语句里的$f[0]
指的就是news表里的title,然后构造一个超链接将title自动更新至首页。
index.php代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>PHP项目实训</title>
</head> <body background="http://cbatl10.club/php/image/7838.jpg"> <table width="1339" height="670" border="0" align="center"> <tr> <td height="67" colspan="2" align="center">北方华创</td> <td colspan="2"><a href="http://cbatl10.club/php/login.php" color:red>登录</a></td> </tr> <tr> <td height="20" colspan="2"><img src="http://cbatl10.club/php/image/1.png" width="1323" hight="10" longdesc="http://cbatl10.club/php/image/2.jpg" /></td> </tr> <tr> <td width="649" height="286"><table width="600" height="86" border="0" align="center"> <tr> <td colspan="2">企业新闻</td> </tr> <tr> <td><a href="https://cbatl.gitee.io">ly0n师傅的博客哎!</a></td> </tr> <tr> <td><a href="https://oneda1sy.gitee.io">da1sy师傅的博客哎!</a></td> </tr> <tr> <?php function linkdatabase(){ try{ $pdo = new pdo("mysql:host=localhost:3306;dbname=mydata","root","******"); return $pdo; }catch(exception $e){ echo $e->getmessage(); } } $pdo = linkdatabase(); $str = "select title,data from news"; $q = $pdo->query($str);
while($f = $q->fetch(PDO::FETCH_NUM)) { echo "<tr>"; echo "<td width=491 height=36><a href='php/news.php?new=".$f[0]."'>".$f[0]."</a></td>"; echo "<td width=491 height=36>".$f[1]."</td>"; } ?> <tr> </table></td> <td width="680"><table width="600" height="100" border="0" align="right"> <tr> <td width="300"><img src="http://cbatl10.club/php/image/3.jpg" width="300" height="200" /></td> <td width="290"><img src="http://cbatl10.club/php/image/4.jpg" width="300" height="200" /></td> </tr> </table></td> </tr> <tr> <td colspan="2" align="center">Copyright @ ly0n</td> </tr>
</body> </html>
|
登录页面代码也没什么好说的,就是纯静态网页,然后调用logincheck.php来验证用户
login.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>PHP项目实训</title> </head>
<body background="http://cbatl10.club/php/image/11.jpg"> <form id="form1" name="form1" method="post" action="logincheck.php"> <table width="389" border="0" align="center"> <tr> <td align="right">用户名:</td> <td><label for="textfield"></label> <input type="text" name="textfield" id="textfield" /></td> </tr> <tr> <td align="right">密码:</td> <td><label for="textfield2"></label> <input type="password" name="textfield2" id="textfield2" /></td> </tr> <tr align="center"> <input name="hid" type="hidden" value="456" /> <td colspan="2" align="center"><input type="reset" name="button" id="button" value="重置" /> <input type="submit" name="button2" id="button2" value="登陆" /></td> </tr> </table> </form>
</body>
</html>
|
登录验证
登陆验证的关键就在于,在调用的数据库之后,去执行sql语句查找在users表里是否有该用户,核心代码
1 2 3
| $username = $_POST["textfield"]; $password = $_POST["textfield2"]; $q = "select * from users where user='".$username."'and password='".$password."'";
|
判断login.php页面提交的username和password是否在表中。如果在则可以去访问发表文章的页面如果不在则还访问login.php页面。代码实现:
1 2 3 4 5
| if($a){ $_SESSION["name"] = $username; echo "<script>window.location='manager.php';</script>"; }else{ echo "<script>window.location='login.php';</script>";
|
logincheck.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>PHP项目实训</title> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登录界面</title> </head> <body> <?php function linkdatabase(){ try{ $pdo = new pdo("mysql:host=localhost:3306;dbname=mydata","root","******-"); return $pdo; }catch(exception $e){ echo $e->getmessage(); } } $p = linkdatabase(); $username = $_POST["textfield"]; $password = $_POST["textfield2"]; $q = "select * from users where user='".$username."'and password='".$password."'"; $k = $p->query($q); $a = $k->fetch(PDO::FETCH_NUM);
if($a){ $_SESSION["name"] = $username; echo "<script>window.location='manager.php';</script>"; }else{ echo "<script>window.location='login.php';</script>"; }
?>
|
读取文章
前端将数据库里的文章title
呈现出来,访问的时候也是需要进行处理的,核心代码:
1 2 3 4
| $r = $q->fetch(PDO::FETCH_NUM); echo "<tr><td heigh=40 align='center'>".$r[0]."<br>发布时间:".$r[2]."</td> </tr>"; echo "<tr><td heigh=300 align='center'>".$r[1]."</td> </tr>"; ?>
|
依旧是利用fetch的特性,$r[0]=title,$r[1]=new,$r[2]=data
就实现了文章的访问,效果图
news.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport"> <title>PHP项目实训</title>
</head> <body background="http://cbatl10.club/php/image/7838.jpg"> <table width="1339" height="251" border="0" align="center"> <tr> <td height="158"><img src="http://cbatl10.club/php/image/5.jpg" width="1323" hight="15" /></td> </tr> <?php $n = $_GET["new"]; function linkdatabase(){ try{ $pdo = new pdo("mysql:host=localhost:3306;dbname=mydata","root","******"); return $pdo; }catch(exception $e){ echo $e->getmessage(); } } $pdo = linkdatabase(); $str = "select * from news where title='".$n."'"; $q = $pdo->query($str); $r = $q->fetch(PDO::FETCH_NUM); echo "<tr><td heigh=40 align='center'>".$r[0]."<br>发布时间:".$r[2]."</td> </tr>"; echo "<tr><td heigh=300 align='center'>".$r[1]."</td> </tr>"; ?> </table>
</body> </html>
|
发表文章
这个功能的实现主要就是要在前端页面调用数据库,并向数据库中写入数据,核心代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| $m=$_POST["textfield"]; $n=$_POST["textarea"]; $t=time(); $data=date("Y-m-d",$t); if($m!=""&&$n!="") { $in="insert into news(title,new,data) values('".$m."','".$n."','".$data."')"; echo $in; $q=$pdo->exec($in); if($q) echo "新闻添加成功"; else echo "新闻添加失败"; }
|
利用sql语句向数据表的每个字段写入内容。写入成功输出添加成功。并在前端显示。
manager.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>新闻提交</title> </head>
<body background="http://cbatl10.club/php/image/11.jpg"> <p><h1 align="center">后台管理页面</h1></p> <form id="form1" name="form1" method="post" action=""> <table width="463" height="195" border="0" align="center"> <tr> <td width="87" align="right">新闻标题:</td> <td width="161"><label for="textfield00"></label> <input type="text" name="textfield00" id="textfield00" /></td> </tr> <tr> <td align="right">新闻内容:</td> <td><label for="textarea"></label> <textarea name="textarea" id="textarea" cols="45" rows="5" ></textarea></td> </tr> <tr> <td colspan="2" align="center"><input type="reset" name="button" id="button" value="重置" /> <input type="submit" name="button2" id="button2" value="提交" /></td> </tr> </table> </form>
<?php $n=$_SESSION["name"]; function linkdatabase() { try{ $pdo=new pdo("mysql:host=localhost:3306;dbname=mydata","root","******"); return $pdo; }catch(exception $e){ echo $e->getmessage(); } } $s="select * from users where user='".$n."'"; $pdo=linkdatabase(); $k=$pdo->query($s); $z=$k->fetch(PDO::FETCH_NUM); if($z) echo "<script>window.location='login.php'</script>"; $m=$_POST["textfield"]; $n=$_POST["textarea"]; $t=time(); $data=date("Y-m-d",$t); if($m!=""&&$n!="") { $in="insert into news(title,new,data) values('".$m."','".$n."','".$data."')"; echo $in; $q=$pdo->exec($in); if($q) echo "新闻添加成功"; else echo "新闻添加失败"; } ?> </body> </html>
|
总结
登录和发表文章的页面都是静态页面,只不过在执行时都会调用数据库,并在数据库中进行操作,还可以将连接数据库的代码写到一个mysql.php文件中,只要调用数据库直接include('./mysql.php')
即可。