php每日学习总结(5)

来源:互联网 发布:系统重装win10软件 编辑:程序博客网 时间:2024/05/29 08:59

//简易留言本制作

设计思路:

1.设计首页面 index.php

<!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" lang = "zh-CN"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>guestbook</title></head><body><form action = "gbook_post.php" method = "post"><p>姓名: <input type = "text" name = "name"><br></p><p>标题: <input type = "text" name = "title"><br></p><p>留言内容: </p><p><textarea type = "text" name = "content" rows = "5" cols = "40"></textarea></p><p><input type = "submit" value = "提交"></p><p><a href = "gbook_sql.php">查看留言</a></p></form></body></html>


 

2.获取表单数据,存入留言 gbook_post.php

<?php$name = $_POST["name"];$title = $_POST["title"];$content = $_POST["content"];require_once("conn.php");$sql = "insert into user1(name,title,content) value('$name','$title','$content')";$tab = mysql_query($sql,$conn);if(!$tab){echo "留言失败!";}else{if(mysql_affected_rows($conn) > 0){echo "留言成功!";}else{echo "没有行数影响";}}mysql_close($conn);//php中尽量用单引号,HTML代码尽量用双引号echo '<br/><a href = "index.php">返回主页</a>';//这里是自动跳转到index页面//echo '<script>location.href = "index.php"</script>';echo '<br/><a href = "gbook_sql.php">查看留言</a>';?>


 

 

问题:

没有做表单验证


///
3.连接数据库 conn.php

<?php//1. connect mysql$conn = mysql_connect("localhost","root","root");if(!$conn){die("connect mysql fail! :".mysql_error());}//2. select dbmysql_select_db("guestbook",$conn);//3. set operation codemysql_query("set names utf8");?>


 


///
4.查看留言gbook_sql.php

<?phpecho '<form action = "" method = "post"><p>你的名字:<input type = "text" name = "name"></p><p><input type = "submit" value = "提交"></p></form>';//@用于抑制@$name = $_POST["name"];// connect mysqlrequire_once('conn.php');$sql = 'select * from user1';//return the result$res = mysql_query($sql,$conn) or die(mysql_error());//display the messagewhile($row = mysql_fetch_row($res)){//var_dump($row);foreach($row as $key => $val){if($name == $row[1]){echo "--$val";}else{break;}}echo "<br/>";}echo '<br/><a href = "index.php">返回主页</a>';//release resourcemysql_free_result($res);//close connectmysql_close($conn);?>

 

问题:

①留言显示不是很友好

②没有做表单验证,导致有主页链接至查看留言页面时,默认输入数据为

空,获取表单数据出错。


/////////////////////////////////////

1.在php页面当中 获取当前页面的表单数据

$name = $_POST["name"];

提示出问题:Notice: Undefined index: name in

搜索解决方案其使用的是get方法

①@$name = $_GET['name'];

然后试了一下也修改一下,居然也成了

修改: @$name = $_POST["name"];

用@来抑制错误

②另一种是进行判断

if( !empty($_GET['name']) ) $name = $_GET['name'];

同样的进行post的修改,没有成功

出现另一个错

Notice: Undefined variable: name

那么就应该是输入为空的情况的错误,使用@忽略这个错误


//
2.php中尽量用单引号,HTML代码尽量用双引号

在php中有html代码时因为引号问题弄得很是混乱,使用上述规则那么问题
就迎刃而解了


//////////////

总结:

在这个简易留言本的制作过程中,才发现自己码的代码太少,有些功能模
块有印象,但是自己就是写不出代码来,还得翻看以前写的代码,还不够
熟练。

 


 

0 0
原创粉丝点击