php与mysql留言板程序实现代码
本程序仅供php+mysql爱好者学习测试用,测试过程中,可能会出现错误,请朋友们根据错误提示修改本程序,本程序为安装版,先运行install目录,然后根据提示操作.
关于PHP语法的那些就不说了,我就说一下连接mysql吧,代码如下:
$conn =@mysql_connect( "localhost" , "root" , "mydown" ) or die ( "数据库链接错误" ); mysql_select_db( "bbs" , $conn ); mysql_query( "set names 'GBK'" );第一句就是描述了连接数据库的语句,并且如果失败的提示]数据库连接错误[;前面的三个参数分别代表了 数据库地址localhost,数据库用户名root,连接数据库密码mydown;
第二句就是描述了连接到数据库表bbs,后面的$conn代表哪个数据库.
第三句话就是转换编码格式,显示中文.
连接好了数据库之后,我们来看下 数据库语句怎么在PHP中提交运行,代码如下:
$sql = "INSERT INTO message (id,user,title,content,lastdate) values ('','$_POST[user]','$_POST[title]','$_POST[content]',now())" ; mysql_query( $sql );上面中的蓝色字体大家应该面熟,意思就是 在数据库中插入信息,第二句是在数据库中运行该语句,想起中类似$_POST[title]的变量,大家应该知道,这是通过网页提交获取的信息.
PHP实例, install安装,代码如下:
<html> <head> <title>留言板安装</title> </head> <body> <form action="check_install.php" method="POST"> <table> <tr><td align="right"><font color="Red">*</font> 输入数据库服务器地址:</td><td><input name="llcc_server" type="text" value="localhost"></td></tr> <tr><td align="right"><font color="Red">*</font> 输入数据库服务器用户名:</td><td><input name="llcc_user_name" type="text" value=""></td></tr> <tr><td align="right"><font color="Red">*</font> 输入数据库服务器密码:</td><td><input name="llcc_user_pass" type="text" value=""></td></tr> <tr><td align="right"><font color="Red">*</font> 输入数据库名称:</td><td><input name="llcc_data_name" type="text" value=""> </td></tr> <tr><td><input type="submit" name="bt_install" value="提交"></td></tr> </table> </form> </body> </html>check_install.php,代码如下:
<?php //获取提交的数据库值 $llcc_server = $_POST [ 'llcc_server' ]; $llcc_user_name = $_POST [ 'llcc_user_name' ]; $llcc_user_pass = $_POST [ 'llcc_user_pass' ]; $llcc_data_name = $_POST [ 'llcc_data_name' ]; //打开数据库配置文件 $fp = fopen ( "config.php" , 'w' ); if (! $fp ) { echo "sorry,try again!" ; } //写入文件内容 $outconfig = "<?phpn" ; $outconfig .= '$server=' ; $outconfig .= "" . $llcc_server . ";//数据库所在IP,如果是本地,默认localhostn" ; $outconfig .= '$server_name=' ; $outconfig .= "" . $llcc_user_name . ";//连接数据库用户名,一般为rootn" ; $outconfig .= '$server_pass=' ; $outconfig .= "" . $llcc_user_pass . ";//连接数据库密码。n" ; $outconfig .= '$server_data=' ; $outconfig .= "" . $llcc_data_name . ";//数据库名n" ; $outconfig .= "?>" ; $fw =fwrite( $fp , $outconfig ); if ( $fw ) { echo "<br><br><br><center>安装完成!请手动删除目录下install文件夹<br><a href=''>返回首页</a></center>" ; } ?>login.php,代码如下:
<?php include ( "header.php" ); include ( "conn.php" ); if ( $_GET [out]){ setcookie( "cookie" , "out" ); echo "<script language=" javascript ">location.href='login.php';</script>" ; } if ( $_POST [id]== 'admin' ){ $pw =md5( $_POST [pw]); if ( $pw == '1b3ae77a2e06580c8cb57e112ddc4a26' ){ setcookie( "cookie" , "ok" ); echo "<script language=" javascript ">location.href='login.php';</script>" ; } } if ( $_COOKIE [ 'cookie' ]!= 'ok' ){ ?> <SCRIPT language=javascript> function Checklogin() { if (myform.id.value== "" ) { alert( "请填写登录名" ); myform.id.focus(); return false; } if (myform.pw.value== "" ) { alert( "密码不能为空" ); myform.pw.focus(); return false; } } </SCRIPT> <table width= "400" > <form action= "" method= "post" name= "myform" onsubmit= "return Checklogin();" > <tr><th>登录名:</th><td><input type= "text" name= "id" /></td></tr> <tr><th>密码:</th><td><input type= "password" name= "pw" /></td></tr> <tr><td colspan= "2" ><input type= "submit" name= "submit" value= "登陆" /></td></tr> </form> </table> <? } else { ?> <a href= '?out=login' >退出</a><form name= "ff" > <input type= "submit" name= "sub" value= "清空数据库" ></form> <?php if ( $_GET [sub]== "清空数据库" ) { $sql = "delete from message" ; $query =mysql_query( $sql ); } ?> <?php } ?>conn.php数据库连接,代码如下:
<?php include ( "config.php" ); $conn =@mysql_connect( $server , $server_name , $server_pass ) or die ( "数据库链接错误" ); mysql_select_db( $server_data , $conn ); //此处代表的要打开的数据库名称,"bbs"是我的数据库名,修改成自己的! mysql_query( "set names 'GBK'" ); //使用GBK中文编码; ?>index.php文件,代码如下:
<?php include ( "conn.php" ); include ( "header.php" ); ?> 自由屋留言板<hr width= "70%" ><table width=750 border= "0" align= "center" cellpadding= "5" cellspacing= "1" bgcolor= "#add3ef" > <?php $sql = "select * from message order by id DESC" ; $query =mysql_query( $sql ); while ( $row =mysql_fetch_array( $query )){ ?> <tr bgcolor= "#eff3ff" > <td><b><?php echo $row [id];?>用户名:</b><?php echo $row [user];?> <b>标题:</b><?php echo $row [title];?> <b>发表时间:</b><?php echo $row [lastdate];?></td> </tr> <tr bgColor= "#ffffff" > <td><b>内容:<?php echo $row [content];?></b></td> </tr> <?php } ?> </table>add.php增加留方程序,代码如下:
<?php include ( "header.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 "留言已提交" ; }; ?><center> <table border= "0" width= "450" > <form name= "add" method= "post" onsubmit= "return CheckPost();" > <tr><th>用户:</th><td><input type= "text" name= "user" size= "10" ></td></tr> <tr><th>标题:</th><td><input type= "text" name= "title" size= "30" ></td></tr> <tr><th>内容:</th><td><textarea name= "content" ></textarea></td></tr> <tr><td colspan= "2" align= "center" ><input type= "submit" name= "submit" value= "发表留言" > <input type= "reset" value= "重设" ></td></tr> </form> </table> </center> <SCRIPT language=javascript> function CheckPost() { if (add.user.value== "" ) { alert( "请填写用户名" ); add.user.focus(); return false; } if (add.title.value.length<5) { alert( "标题不能少于5个字符" ); add.title.focus(); return false; } if (add.content.value== "" ) { alert( "必须要填写留言内容" ); add.content.focus(); return false; } } </SCRIPT>test.php数据库结构,代码如下:
<?php include( "config.php" ); include( "conn.php" ); $sql=" CREATE TABLE `message` ( `id` tinyint(1) NOT NULL auto_increment, ` user ` varchar (25) NOT NULL , `title` varchar (50) NOT NULL , `content` tinytext NOT NULL , `lastdate` date NOT NULL , PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk AUTO_INCREMENT=1 ;"; $query=mysql_query($sql); if($query){ echo "执行成功" ;} else echo "执行失败!" ; ?>查看更多关于php与mysql留言板程序实现代码 - 综合实例的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did3795