首页 > 编程地带 > PHP-简易数据库操作实例—书目管理系统

PHP-简易数据库操作实例—书目管理系统

2007年11月1日 发表评论 阅读评论

  根据前几天学习的内容,今天终于完成了一个简单的数据库操作的实例—我的书房书库管理系统。写了三个小时,哎。。。效率有点低。。。
本文没有技术含量,仅供参考!
一共分为八个文件:

<br /><?php<br />  //数据库环境变量:sys_conf.inc<br />  class sys_conf{<br />    public static $dbhost="localhost";<br />    public static $dbuser="root";<br />    public static $dbpwd="password";<br />    public static $dbname="test";<br />  }<br />?>


[codes]

    require_once("sys_conf.inc");
    class database
    {
        //属性
        private $host;
        private $user;
        private $pwd;
        private $name;
        private $connection;
        
        //方法
        //获取属性值
        function _get($property_name){
            if(isset($this->$property_name)){
                return($this->$property_name);
            }
            else {
                return(NULL);
            }
        }
        
        //设置属性值
        function _set($property_name,$value){
            $this->$property_name=$value;
        }
        
        //建立链接
        function _construct()
        {
            $this->host=sys_conf::$dbhost;
            $this->user=sys_conf::$dbuser;
            $this->pwd=sys_conf::$dbpwd;
            $this->name=sys_conf::$dbname;
            $this->connection=mysql_connect($this->host,$this->user,$this->pwd);
            mysql_select_db($this->name,$this->connection);
        }
        
        
        //断开链接
        function _destruct()
        {
            mysql_close($this->connection);
        }
        
        //增删改
        function execute($sql)
        {
          mysql_query($sql);
        }
        
        
        function query($sql){
            $result_array=array();
            $i=0;
            $query_result=mysql_query($sql,$this->connection);
            while($row=mydql_fetch_object($query_result))
            {
                $result_array[$i++]=$row;
            }
            return $result_array;
        }
    }
?>[/code]

<br /><!---图书类文件:class_books.php ----><br /><?php<br />  require("class_database.php");<br />  class books<br />  {<br />    //属性<br />    private $id;<br />    private $name;<br />    private $price;<br />    private $author;<br />    <br />    <br />    //方法<br />    function _get($property_name){<br />        if(isset($this->$property_name)){<br />            return($this->$property_name);<br />        }<br />        else{<br />            return(NULL);<br />        }<br />    }<br />    <br />    <br />    function _set($property_name,$value){<br />        $this->property_name=$value;<br />    }<br />    <br />    <br />    function _construct()<br />    {<br />    }<br />    <br />    <br />    //add<br />    function add(){<br />        $db=new database;<br />        $sql="insert into computers (name,price,author) values ";<br />        $sql.="(&#039;$this->name&#039;,$this->price,&#039;$this->author&#039;)";<br />        $db->execute($sql);<br />        $db=NULL;<br />    }<br />    <br />    <br />    //update<br />    function update(){<br />        $db=new database;<br />        $sql="update computers set ";<br />        $sql.="name=&#039;this->name&#039;,price=$this->price,author=&#039;$this->author&#039;";<br />        $sql.="where id=$this->id";<br />        $db->execute($sql);<br />        $db->NULL;<br />    }<br />    <br />    <br />    //delete<br />    function delete(){<br />        $db=new database;<br />        $sql="delete from computers ";<br />        $sql.="where id=&#039;$this->id&#039;";<br />        $db->execute($sql);<br />        $db=NULL;<br />    }<br />    <br />    <br />    //query<br />    static function query($condition){<br />      if($condition=="" &#124;&#124; $condition==NULL) $condition="";<br />      else $condition="where ".$condition;<br />      $db=new database;<br />      $sql="select * from computers ".$condition;<br />      $arr_books=$db->query($sql);<br />      return $arr_books;<br />      $db=NULL;<br />    }<br />    <br />  }<br />?>

<br /><br /><!---主文件index.php---><br /><html><br />  <head><title>woyigui的书房</title></head><br />  <Body><br />  <h1>woyigui的书房</h1><br />  <form method=POST action="index.php"><br />    书名:<input type=text name=book_name><br />    价格:<input type=text name=book_price><br />    作者:<input type=text name=book_author><br />    <input type=submit name=submit value="查询书目"><br />    <br><br />    <?php<br />    include_once("show_books.php");<br />    include_once("class_database.php");<br />    switch($_POST&#91;"submit"&#93;){<br />      case "查询书目":<br />          $cond.=" id>0";<br />          if($_POST&#91;book_name&#93;!="") $cond=$cond." and name=&#039;$_POST&#91;book_name&#93;&#039;";<br />          if($_POST&#91;book_price&#93;!="") $cond=$cond." and name=&#039;$_POST&#91;book_price&#93;&#039;";<br />          if($_POST&#91;book_author&#93;!="") $cond=$cond." and name=&#039;$_POST&#91;book_author&#93;&#039;";<br />          show($cond);<br />          break;<br />      case "添加新书":<br />          include_once("add_books.php");<br />          show_add_form();<br />          break;<br />      case "添加":<br />          include_once("add_books.php");<br />          add_books();<br />          show("");<br />          break;<br />      case "修改书目":<br />          echo "<br><input type=hidden name=updata_book_id value=$_POST&#91;selected_book_id&#93;>";<br />          include_once("update_book.php");<br />          show_update_form();<br />          break;<br />      case "修改":<br />          include_once("update_books.php");<br />          update_books($_POST&#91;"update_book_id"&#93;);<br />          show("");<br />          break;<br />      case "删除书目":<br />          echo $_POST&#91;"selected_book_id"&#93;;<br />          include_once("delete_books_id");<br />          delete_books($_POST&#91;"selected_book_id"&#93;);<br />          show("");<br />          break;<br />     default:<br />        show("");<br />        break;<br />   }<br />  ?><br /> </form><br /> </body><br /></html>    <br />

<br /><!--添加页面:add_books.php--><br /><?php<br />    function show_add_form()<br />    {<br />        echo "<br />          <h3>请输入新图书信息:</h3><br><br />          书名:<input type=text name=add_book_name>*<br><br />          书目:<input type=text name=add_book_price>*<br><br />          作者:<input type=text name=add_book_author>*<br><br />          <input type=submit name=submit value=&#92;"添加&#92;"><br />          <input type=reset value=&#92;"重写&#92;"><br />          <br><br />          ";<br />     }<br />     <br />     <br />     function add_books()<br />     {<br />        include_once("class_books.php");<br />        if($_POST&#91;submit&#93;=="添加")<br />        {<br />            if($_POST&#91;add_book_name&#93;=="" &#124;&#124; $_POST&#91;add_book_price&#93;=="" &#124;&#124; $_POST&#91;add_book_author&#93;=="")<br />            {<br />                echo "<font color=red>添加失败,请把信息填写完整</font><br>";<br />                echo "<a href=&#92;"javascript:history.go(-1)&#92;">重试</a>";<br />            }<br />            else{<br />                $b=new books;<br />                $b->_set(name,$_POST&#91;add_book_name&#93;);<br />                $b->_set(price,$_POST&#91;add_book_price&#93;);<br />                $b->_set(author,$_POST&#91;add_book_author&#93;);<br />                $b->add();<br />                echo "图书$_POST&#91;book_name&#93;添加成功!<br>";<br />            }<br />        }<br />     }<br />?>
<br /><!---修改页面:updata_books.php---><br /><br /><?php<br />    function show_update_form(){<br />        echo "<br />          <h3>请输入新图书信息:</h3><br><br />          书名:<input type=text name=add_book_name>*<br><br />          书目:<input type=text name=add_book_price>*<br><br />          作者:<input type=text name=add_book_author>*<br><br />          <input type=submit name=submit value=&#92;"添加&#92;"><br />          <input type=reset value=&#92;"重写&#92;"><br />          <br><br />          ";<br />    }<br />    <br />    function update_books($book_id)<br />     {<br />        include_once("class_books.php");<br />        global $update_book_id;<br />        if($_POST&#91;submit&#93;=="修改")<br />        {<br />            if($_POST&#91;update_book_name&#93;=="" &#124;&#124; $_POST&#91;update_book_price&#93;=="" &#124;&#124; $_POST&#91;update_book_author&#93;=="")<br />            {<br />                echo "<font color=red>修改失败,请把信息填写完整</font><br>";<br />                echo "<a href=&#92;"javascript:history.go(-1)&#92;">重试</a>";<br />            }<br />            else{<br />                $b=new books;<br />                $b->_set(id,$book_id);<br />                $b->_set(name,$_POST&#91;update_book_name&#93;);<br />                $b->_set(price,$_POST&#91;update_book_price&#93;);<br />                $b->_set(author,$_POST&#91;update_book_author&#93;);<br />                $b->update();<br />                echo "图书$_POST&#91;book_name&#93;修改成功!<br>";<br />            }<br />        }<br />    }<br />?>

<br /><!---删除页面:delete-books.php----><br /><?php<br />    include_once("class_books.php");<br />    function delete_books($book_id){<br />        include_once("class_books.php");<br />        if($book_id==""){<br />            echo "<font color=red>请选择想要删除的图书!</font><br>";<br />        }<br />        else{<br />            $b=new books;<br />            $b->_set(id,$book_id);<br />            $b->_delete();<br />            echo "删除成功!<br>";<br />        }<br />    }<br />?>

<br /><!----显示页面:show_books.php-----><br /><br /><?PHP<br />    include_once("class_books.php");<br />    echo "<br />        <input type=submit name=submit value=&amp;#92;"添加新书&amp;#92;"><br />        <input type=submit name=submit value=&amp;#92;"修改书目&amp;#92;"><br />        <input type=submit name=submit value=&amp;#92;"删除书目&amp;#92;"><br />    ";<br />    <br />    function show($cond){<br />        $b=new books;<br />        echo "<br />            <table border=1><br />                <tr><br />                    <td></td><br />                    <td><b>名称</b></td><br />                    <td><b>价格</b></td><br />                </tr><br />       ";<br />       $all_books=books::query($cond);<br />       foreach($all_books as $item)<br />       {<br />          echo "<tr>";<br />          echo "<td><input type=checkbox name=selected_book_id value=$item->id></td>";<br />          echo "<td>".$item->name."</td>";<br />          echo "<td>".$item->price."</td>";<br />          echo "<td>".$item->author."</td>";<br />       }<br />       echo "</table>";<br />   }<br />?>

转载请注明:woyigui's blog [http://www.woyigui.cn/]
本文标题:PHP-简易数据库操作实例—书目管理系统
本文地址:http://www.woyigui.cn/2007/11/01/php-simple-examples-of-database-operations-bibliography-management-system/

分类: 编程地带 标签: ,
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
*