小詞典(php)

  1. 查詢前臺頁面(enword.php)
    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

<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)mysql

<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語句代碼。sql

<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操做也是相當重要的哦!ide

  1. 建立數據庫
    函數

create database php_dic;post

2.建立一張表ui

create table dic(this

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;

而後就能夠查詢和添加啦~~

相關文章
相關標籤/搜索