我的第一个PHP+MYSQL示例
所属栏目:计算机应用论文
发布时间:2011-02-22 16:13:46 更新时间:2011-02-22 16:13:46
PHP+MYSQL示例
<HTML>
<HEAD>
<title>MySQL数据库操作示例</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数据库操作示例:</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()
{
//连接数据库服务器
$db_id = mysql_pconnect("localhost", "bgt", "");
//选择数据库
mysql_select_db('test');
//$sql="SELECT * FROM `tt1` LIMIT 0 , 30 ";
//$result = mysql_db_query("test",$sql,$db_id);
////设定每一页显示的记录数
$pagesize=15;
//取得记录总数,计算总页数用
$res=mysql_query("select count(*) from tt1 " ,$db_id);
$myrow = mysql_fetch_array($res);
$numrows=$myrow[0];
//计算总页数
$pages=intval($numrows/$pagesize);
if ($numrows%$pagesize)
$pages++;
//判断页数设置与否,如无则定义为首页
$page=$_REQUEST['page'];
if (!isset($page))
$page=1;
//判断转到页数
if (isset($ys))
if ($ys>$pages)
$page=$pages;
else
$page=$ys;
//计算记录偏移量
$offset=$pagesize*($page-1);
//取记录,执行查询
$result=mysql_query("select * from `tt1` order by id desc limit $offset,$pagesize" ,$db_id);
//循环输出查询结果
//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('确实要删除吗');">删除</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);
//关闭连接
mysql_close();
}
?>
<?php
function newData()
{
?>
<form name="form1" method="post" action="">
<table width="90%" border="0" align="center" bgcolor="#CCCCCC">
<tr>
<td>插入数据: 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()
{
//连接数据库服务器
$db_id = mysql_pconnect("localhost", "bgt", "");
//选择数据库
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();
//执行插入查询
mysql_db_query("test",$sql,$db_id);
//关闭连接
mysql_close();
}
?>
<?php
function editData()
{
//连接数据库服务器
$db_id = mysql_pconnect("localhost", "bgt", "");
//选择数据库
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>编辑数据: 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()
{
//连接数据库服务器
$db_id = mysql_pconnect("localhost", "bgt", "");
//选择数据库
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();
//执行插入查询
mysql_db_query("test",$sql,$db_id);
//关闭连接
mysql_close();
}
?>
<?php
function deleteARow()
{
//连接数据库服务器
$db_id = mysql_pconnect("localhost", "bgt", "");
$sql="delete from `tt1` where id=".$_REQUEST['itemid'];
//执行删除
mysql_db_query("test",$sql,$db_id);
//关闭连接
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>
月期刊平台服务过的文章录用时间为1-3个月,依据20年经验,经月期刊专家预审通过后的文章,投稿通过率100%以上!