php学习_简单留言板2013.01.04
今天跟着视频写了一个留言板:
add.php
<?php include("conn.php"); ?> <form action="list.php" method="post"> 用户:<input type="text" size="10" name="user"/><br> 标题:<input type="text" name="title"/><br> 内容:<textarea name="content"></textarea><br> <input type="submit" name="submit" value="发布留言"> </form>
conn.php
<?php $conn = @ mysql_connect("localhost", "123", "123") or die("数据库链接错误"); mysql_select_db("bbs", $conn); mysql_query("set names 'GBK'"); //使用GBK中文编码; ?>
list.php
<?php include("conn.php"); if($_POST['submit']){ $sql="insert into message (id,user,title,content,lastdate) " . "values('','$_POST[user]','$_POST[title]','$_POST[content]',now())"; mysql_query($sql); echo "发布成功"; } ?> <table width=500 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef"> <? $sql="select * from message"; $query=mysql_query($sql); while($row=mysql_fetch_array($query)){ ?> <tr bgcolor="#ef00ff"> <td>标题:<?=$row[title]?></td> </tr> <tr bgColor="#00ffff"> <td> 用户:<?=$row[user]?></td> </tr> <tr bgColor="#ffffff"> <td>内容:<?=$row[content]?></td> </tr> <? } ?> </table>
//在php setting里面打开short open tag之后,就支持短标签格式了,可直接用<?...>代替
php和jsp真的很像。php里面也可以插入html标签,只是它用include来进行页面跳转。它和mysql连接语句也是写在.php文件里面,里面嵌入的html标签可以提交一个客户端信息,在mysql里面有相应的表之后,同样通过form标签来提交到数据库。将信息插入到数据库的代码也是在.php文件(php标签)中编写。
即.php文件的php标签中处理了连接数据库、查询数据库、页面跳转等;
.php文件的html标签中处理显示页面