小辞典(php)


  • フロントページ(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;
    あとで検索して追加できます~~