能解析PHP(包含MySQL数据库)的本地环境
任意一款文本编辑器
任意一款网页浏览器
我们先把表单的代码写出来:
接下来,创建mysql数据表,表的名称为liuyan:
接下来,我们用另一个
接下来,我们书写CSS样式表,代码如下:*{margin:0;padding:0;}span{display:block; //以区块显示text-align:center; //居中对齐margin-top:20px; //向上挤外边20pxfont-size:22px; //字体大小22pxfont-weight:bold; //字体加粗margin-bottom:15px; //向下挤下边15px}div{width:300px;height:80px;margin:0 auto;font-weight:bold; }textarea{width:300px; //宽300pxheight:80px; //高80pxbackground:#eee;margin-bottom:10px;}form input{margin-top:10px;width:110px;height:18px;background:#eee;}form input.butt{width:60px;height:28px;font-weight:bold;}#show{margin-top:100px;}#show table{width:380px;margin:0 auto;}#show table td{text-align:center;}
PHP处理页面代码如下:alert('留言内容不能为空');history.back();'; //弹窗}else{ $u=$_POST['user']; //获取表单提交过来的user的值if(empty($u)){ //如果$u为空echo ''; //弹窗}else{$t=date('Y-m-d,H:m:s'); //获取时间$c=trim($c); //去掉两端的空格$c=htmlspecialchars($c); //把html标签转换为实体$c=addslashes($c); //转义$cc=mysqli_connect('localhost','root',''); //连接MySQL数据库$db=mysqli_select_db($cc,'test'); //选择test数据库$sql='insert into liuyan (name,content,time) values ('$u','$c','$t')';$r=mysqli_query($cc,$sql); //执行SQL语句if($r){ //如果SQL语句执行成功echo ''; //弹窗}else{echo ''; //弹窗}mysqli_close($cc); //关闭数据库的连接}}?>
PHP处理的过程如图27-2,27-3:
要使用mysqli_connect,必须要开启mysqli模块