查询前台页面(enword.php)
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> </head> <body> <img src="1.png"/> <h1>英汉词典</h1> <form action="enw.php" method="post"> 请输入英文:<input type="text" name="enword" /> <input type="submit" value="查询" name="serch" /> </form> </body> </html>
2.编写数据库操作类(ewTool.class.php)
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> </head> <body> <?php class enwTool { protected $conn; private $host="127.0.0.1"; private $user="root"; private $password=""; protected $bd="php_dic"; //构造函数声明变量 public function __construct() { $this->conn=mysql_connect($this->host,$this->user,$this->password) or die("链接失败"); mysql_select_db($this->bd); mysql_query("set names utf8"); } //查询数据库语句 public function dql($sql) { //$res为资源型 $res=mysql_query($sql,$this->conn) or die(mysql_error()); return $res; } //添加数据库语句 public function dml($sql) { $b=mysql_query($sql,$this->conn); if(!$b) { return 0; } else { if(mysql_affected_rows($this->conn)) { return 1; } else { return 2; } } } } ?> </body> </html>
3.编写添加前台页面(add.php)
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> </head> <body> <h1>请添加英汉</h1> <form action="dml.php" method="post"> 请输入英文:<input type="text" name="eng"/> 请输入中文:<input type="text" name="chi"> <input type="submit" value="添加"> </form> </body> </html>
4.便面dml语句代码。
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> </head> <body> <?php require_once "enwTool.class.php"; $eng=$_REQUEST[‘eng‘]; $chi=$_REQUEST[‘chi‘]; if(isset($eng)||isset($chi)) { $sql="insert into dic (enword,chword) values (‘$eng‘,‘$chi‘)"; $p=new enwTool(); $res=$p->dml($sql); if($res==0) { echo "老兄没有你没有写东西吧!"; } else if($res==1) { echo "恭喜添加成功!"; echo "<a href=‘enword.php‘>返回</a>"; } else if($res==2) { echo "你添加有误!"; } } else { echo"请输入"; echo"<a herf=‘add.php‘>返回</a>"; } ?> </body> </html>
以上就是php代码啦。
mysql操作也是至关重要的哦!
创建数据库
create database php_dic;
2.创建一张表
create table dic(
dic_id int primary key auto_increment,
enword varchar(20) not null default‘‘,
chword varchar(200) not ull default‘‘
)set character utf8;
3.设置字符集
set names utf8;
4.设置数据库表编码和表编码
alter database php_dic character set utf8;
alter table dic character set utf8;
然后就可以查询和添加啦~~
原文地址:http://7856933.blog.51cto.com/7846933/1684994