<HTML>
<HEAD>
<title>MySQL數(shù)據(jù)庫操作示例</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="default.css" rel="stylesheet" type="text/css">
<style type="text/css">
.bg01 { BACKGROUND-POSITION: 50% bottom; BACKGROUND-IMAGE: url(../about/images/htcw_032.gif); BACKGROUND-REPEAT: no-repeat }
</style>
</HEAD>
<body bgcolor="#666666" leftmargin="0" topmargin="0">
<table width="90%" border="0" align=center>
<tr align="center" valign="middle">
<td style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#4B92D9', endColorStr='#ffffff', gradientType='1')">
<div align="left"><strong><font color="#ffffff" size="3">MySQL數(shù)據(jù)庫操作示例:</font></strong></div></td>
</tr>
</table>
<table width="90%" border="0" align="center" cellspacing="0" bgcolor="#CCCCCC">
<tr>
<td height="40"><div align="center">
<table width="50%" border="0">
<tr>
<td><div align="right"><strong><a href="index.php?act=refresh">刷新</a></strong></div></td>
<td> </td>
<td><strong><a href="index.php?act=new">插入</a></strong></td>
</tr>
</table>
<strong></strong></div> <div align="center"><strong></strong></div> <div align="center"><strong></strong></div> <div align="center"></div></td>
</tr>
</table>
<?php
function showData()
{
//連接數(shù)據(jù)庫服務(wù)器
$db_id = mysql_pconnect("localhost", "bgt", "");
//選擇數(shù)據(jù)庫
mysql_select_db('test');
//$sql="SELECT * FROM `tt1` LIMIT 0 , 30 ";
//$result = mysql_db_query("test",$sql,$db_id);
////設(shè)定每一頁顯示的記錄數(shù)
$pagesize=15;
//取得記錄總數(shù),計(jì)算總頁數(shù)用
$res=mysql_query("select count(*) from tt1 " ,$db_id);
$myrow = mysql_fetch_array($res);
$numrows=$myrow[0];
//計(jì)算總頁數(shù)
$pages=intval($numrows/$pagesize);
if ($numrows%$pagesize)
$pages++;
//判斷頁數(shù)設(shè)置與否,如無則定義為首頁
$page=$_REQUEST['page'];
if (!isset($page))
$page=1;
//判斷轉(zhuǎn)到頁數(shù)
if (isset($ys))
if ($ys>$pages)
$page=$pages;
else
$page=$ys;
//計(jì)算記錄偏移量
$offset=$pagesize*($page-1);
//取記錄,執(zhí)行查詢
$result=mysql_query("select * from `tt1` order by id desc limit $offset,$pagesize" ,$db_id);
//循環(huán)輸出查詢結(jié)果
//echo "<br><br>\n";
echo "<table width=90% align=center bgcolor=#aaaaaa border=0 cellspacing=0 cellpadding=0>\n";
echo "<tr align=center><td>ID</td><td>P_ID</td><td>NAME</td><td>EDIT</td><td>DELETE</td></tr>\n";
$i=0;
while($row = mysql_fetch_array($result)) {
if ( $i%2==0)
$altercolor="#ffffff";
else
$altercolor="#eeeeee";
echo "<tr align=center bgcolor=$altercolor><td>". $row[0]."</td>\n";
echo "<td>". $row["p_id"]."</td>\n";
//echo " ";
echo "<td>".$row["name"]."</td>"
?>
<td><a href="index.php?act=edit&itemid=<?php echo $row[0]?>">編輯</a></td>
<td><a href="index.php?act=delete&itemid=<?php echo $row[0]?>" onClick="javascript:return confirm('確實(shí)要?jiǎng)h除嗎');">刪除</a> </td>
</tr>
<?php
$i++;
}
echo "</table>\n";
echo "<table width=90% align=center bgcolor=#aaaab3 border=0 cellspacing=0 cellpadding=0>\n";
echo "<tr align=center><td>共".$numrows."記錄</td><td>".$page."/".$pages."頁</td>";
if (($page-1)>0)
$pre=($page-1);
else
$pre=1;
if (($page+1)>$pages)
$next=$pages;
else
$next=($page+1);
echo "<td><a href=index.php?page=1>首頁</a></td><td><a href=index.php?page=". $pre.">上頁</a></td>";
echo "<td><a href=index.php?page=". $next.">下頁</a></td><td><a href=index.php?page=".$pages.">尾頁</a></td></tr>\n";
echo "</table>";
mysql_free_result($result);
//關(guān)閉連接
mysql_close();
}
?>
<?php
function newData()
{
?>
<form name="form1" method="post" action="">
<table width="90%" border="0" align="center" bgcolor="#CCCCCC">
<tr>
<td>插入數(shù)據(jù): P_ID:</td>
<td><input name="p_id" type="text" id="p_id"></td>
<td>NAME: </td>
<td><input name="name" type="text" id="name"></td>
<td>
<input type="submit" name="Submit2" value="插入">
<input name="act" type="hidden" id="act" value="upnew"></td>
</tr>
</table>
<div align="center"></div>
</form>
<?php
}
?>
<?php
function upNewData()
{
//連接數(shù)據(jù)庫服務(wù)器
$db_id = mysql_pconnect("localhost", "bgt", "");
//選擇數(shù)據(jù)庫
mysql_select_db('test');
$pid=$_REQUEST['p_id'];
$name=$_REQUEST['name'];
$sql="insert into `tt1`(p_id,name) values('".$pid ;
$sql.="','";
$sql.=$name;
$sql.="')";
//echo($sql);
//die();
//執(zhí)行插入查詢
mysql_db_query("test",$sql,$db_id);
//關(guān)閉連接
mysql_close();
}
?>
<?php
function editData()
{
//連接數(shù)據(jù)庫服務(wù)器
$db_id = mysql_pconnect("localhost", "bgt", "");
//選擇數(shù)據(jù)庫
mysql_select_db('test');
$sql="SELECT * FROM `tt1` where id=".$_REQUEST['itemid'];
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
?>
<form name="form1" method="post" action="">
<table width="90%" border="0" align="center" bgcolor="#FFFFFF">
<tr>
<td>編輯數(shù)據(jù): P_ID:</td>
<td><input name="p_id" type="text" id="p_id" value="<?php echo $row[1]?>"></td>
<td>NAME:</td>
<td><input name="name" type="text" id="name" value="<?php echo $row[2]?>"></td>
<td>
<input type="submit" name="Submit" value="編輯">
<input name="act" type="hidden" id="act" value="upedit">
<input name="id" type="hidden" id="id" value="<?php echo $row[0]?>"></td>
</tr>
</table>
<div align="center"></div>
</form>
<?php
mysql_close();
}
?>
<?php
function upEditData()
{
//連接數(shù)據(jù)庫服務(wù)器
$db_id = mysql_pconnect("localhost", "bgt", "");
//選擇數(shù)據(jù)庫
mysql_select_db('test');
$id= $_REQUEST['itemid'];
$pid=$_REQUEST['p_id'];
$name=$_REQUEST['name'];
$sql="update `tt1` set p_id='".$pid ."',name='".$name ."' where id=".$id;
//$sql.="','";
//$sql.="')";
//echo($sql);
//die();
//執(zhí)行插入查詢
mysql_db_query("test",$sql,$db_id);
//關(guān)閉連接
mysql_close();
}
?>
<?php
function deleteARow()
{
//連接數(shù)據(jù)庫服務(wù)器
$db_id = mysql_pconnect("localhost", "bgt", "");
$sql="delete from `tt1` where id=".$_REQUEST['itemid'];
//執(zhí)行刪除
mysql_db_query("test",$sql,$db_id);
//關(guān)閉連接
mysql_close();
}
?>
<?php
//if ($HTTP_GET_VARS['act']!="")
$requestAct=$_REQUEST['act'];
//echo ($requestAct);
//die();
switch ($requestAct)
{
case "new":
newData();
break;
case "upnew":
upNewData();
break;
case "edit":
editData();
break;
case "upedit":
upEditData();
break;
case "delete":
deleteARow();
break;
default:
//showData();
break;
}
showData();
?>
</body>
</HTML>
轉(zhuǎn)載請(qǐng)注明來自:http://www.jinnzone.com/jisuanjiyingyonglw/152.html
上一篇:Mysql的帳號(hào)問題
下一篇:php 新增函數(shù)