由mysql資料庫基礎上的php程式實現單詞的查詢、刪除、更改和查詢

來源:互聯網
上載者:User

我做了一個php程式,將表單資料添加到資料庫,借用mysql擴充庫函數實現對mysql資料庫的操作,能夠實現添加單詞、刪除單詞、更新和查詢單詞。運行環境是普通的mysql資料庫和php、Apache伺服器。這個程式非常簡單,屬於那種一看就懂的程式,不過還是要提醒一句,像那個資料庫和資料表肯定事先要存在!本文用的資料庫是test,資料表示test資料庫下的表名為danci的資料表,一共有三個屬性:id danci dt 分別是int、char、timestamp類型。直接貼代碼:

使用者看到的介面:

<!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"><head><title>www.codeke.com</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="keywords" content="php video,javascript" /><meta http-equiv="description" content="php php php hp php" /><style type="text/css"><!--span { margin:1px 5px;}--></style></head><form action="pwd.php" method="post"><table cellpadding="0" cellspacing="0" border="1px" cols="5" width="300"><tr><td align="center" colspan="2">列表</td><td colspan="2" align="center">名稱</td></tr><tr><td align="center"  valign="bottom" height="20px" rowspan="2" colspan="3">輸入單詞</td><td colspan="2" rowspan="2" valign="middle" height="20px" align="right"><input size="26" type="text" name="danci" /></td></tr><tr></tr><tr id="thanks" style="display:none"><td colspan="5"><span>輸入舊單詞</span><span><input size="26" type="text" name="jiudanci" value="" /></span></td></tr><tr></tr><tr><td align="center" valign="bottom" height="20px" colspan="3" >選擇操作類型</td><td valign="middle" height="20px" align="right"><select  name="select" id="opt" onchange="javascript:change();"><option value="1" selected="selected">添加</option><option value="2">刪除</option><option value="3">更改</option><option value="4">查詢</option></select></td></tr><script type="text/javascript">window.onload=change;function change(){var temp = document.getElementById("opt").value;if(temp==3){document.getElementById("thanks").style.display="table-row";}else{document.getElementById("thanks").style.display="none";}}</script><tr><td colspan="2"><input type="reset" /></td><td align="center" colspan="2"><input type="submit" value="submit" /></td></tr></table> </form>


處理表單資料的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"><head><title>www.codeke.com</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><!--meta http-equiv="refresh" content="3;url=access.htm" /--></head> <?php   require_once 'def.class.php'; //這個檔案用於資料庫操作  $danci=$_POST['danci'];  $jiudanci=$_POST['jiudanci'];  $choice=$_POST['select'];   $danci=strtoupper($danci);  //將單詞統一轉化成大寫的  $jiudanci=strtoupper($jiudanci);  $sqlQue=new SqlDiy("like","admin");  $sqlQue->setDanci($danci);  $sqlQue->setJiudanci($jiudanci);  switch($choice){case 1:$sqlQue->setOper("insert");break; case 2:$sqlQue->setOper("delete");break;case 3:$sqlQue->setOper("update");break;case 4:$sqlQue->setOper("select");break;  }  if($danci==$jiudanci&&$choice==3){  echo "新單詞和舊單詞一樣,無需更新";    }  if($danci==""||($jiudanci==""&&$choice==3)){echo "輸入為空白";return ;  }    $sqlQue->connSql();  ?>


封裝了串連資料庫的類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"><head><title>www.codeke.com</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head> <?php    //header("content-type:text/html;charset=utf-8");     class SqlDiy{    private $time;    private $pwd;    private $danci;    private $user;    private $jiudanci;    private $oper;    function __construct($user,$pwd){    $this->user=$user;    $this->pwd=$pwd;    $this->timeWorkErr();    }    function timeWorkErr(){    $this->time=time();    date_default_timezone_set('PRC');    }    function connSql(){        $conn=mysql_connect("localhost",$this->user,$this->pwd);    if(!$conn){    echo mysql_error().error_log(mysql_error.date("m月d日 H:i:s",$this->time)."\r\n",3,"c:/error.txt");    }    mysql_select_db("test",$conn) or error_log(mysql_error.date("m月d日 H:i:s",$this->time)."\r\n",3,"c:/error.txt");    mysql_query("set names utf8");    switch($this->oper){    case "select":$query="select * from `danci` where danci='$this->danci' limit=0,1";break; //一般不要用*,需要哪個欄位就選擇哪個欄位    case "delete":$query="delete from `danci` where danci='$this->danci'";break;    case "insert":$query="insert into `danci`(danci)values('$this->danci')";break;    case "update":$query="update `danci` set `danci`='$this->danci' where `danci`='$this->jiudanci'";break;    }    //$query="update user set name='lily' where name='lily'";    mysql_query("select * from `danci` where danci='$this->danci'",$conn);            if(mysql_affected_rows($conn)>0&&$this->oper=="insert"){    echo "<br />單詞庫中已經有該單詞,無需添加";     return ;    }      if(mysql_affected_rows($conn)<1&&$this->oper=="select"){    echo "<br />單詞庫裡沒有該單詞"; //針對查詢操作    return ;    }    if(mysql_affected_rows($conn)<1&&$this->oper=="delete"){    echo "<br />無法刪除一個沒有的單詞"; //針對刪除操作    return ;    }    $res=mysql_query($query,$conn);    if(mysql_affected_rows($conn)<1){    echo "<br />串連資料庫有誤$this->jiudanci--$this->danci";    error_log(mysql_error().date("m月d日 H:i:s",$this->time)."\r\n",3,"c:/error.txt");    }    else {    if($this->oper=="select"){      while($rows=mysql_fetch_object($res)){     echo "<br />".$rows->id."--".$rows->danci."--".$rows->dt;    }     mysql_free_result($res);    }    if($this->oper=="insert"){    echo "<br />插入單詞成功";    }    if($this->oper=="delete"){    echo "<br />刪除單詞成功";    }    if($this->oper=="update"){    echo "<br />更新單詞成功";    }    }        mysql_close($conn);    }    function setJiudanci($i){    $this->jiudanci=$i;    }    function setOper($i){    $this->oper=$i;    }    function setDanci($i){    $this->danci=$i;    }            }    ?>

建立資料庫的函數:

function createDataBase(){ //建立一個資料表    $link=mysql_connect('localhost',"root",$password);//必須用root帳號和密碼登入    if(!$link){    die('Could not connect: ' . mysql_error());    }      $sql = 'CREATE DATABASE my_db';    if(mysql_query($sql, $link)) {    echo "Database my_db created successfully\n";    } else {    echo 'Error creating database: '.mysql_error()."\n";    }        mysql_close($link);     }

刪除資料庫的函數:

function deleteDataBase(){ //刪除一個資料表    $link=mysql_connect('localhost',"root",$password);//必須用root帳號和密碼登入    if(!$link){    die('Could not connect:'.mysql_error());    }      $sql = 'DROP DATABASE discuz';    if(mysql_query($sql, $link)) {    echo "Database discuz droped successfully\n";    } else {    echo 'Error droping database: '.mysql_error() . "\n";    }            mysql_close($link);     }




相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.