php实现mysql数据库连接操作及用户管理
文件列表。。文件内容。。
dbconn.php
userListt.php
editUser.php
editDo.php
detailUser.php
deleteUser.php
addUser.php
addDo.php
<dbconn.php>
<?php //创建数据库连接 $con=mysql_connect("localhost",'root','')ordie('error:'.mysql_error()); mysql_select_db('hyxx',$con)ordie('error:'.mysql_error()); mysql_query('setNAMESutf8'); ?>
userListt.php
<html> <head> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"> <title>会员信息列表</title> </head> <body> <tableborder="1"cellspacing="0"cellpadding="0"id="userList"align="center"> <tralign="center"> <td>id</td> <td>用户名</td> <td>密码</td> <td>性别</td> <td>年龄</td> <td>出生年月</td> <td>爱好</td> <td>注册时间</td> <td>最后登录时间</td> <td>操作</td> </tr> <?php require_once'inc/dbConn.php'; //这是啥东东。。 date_default_timezone_set("PRC"); //读数据。。。 $sql="select*fromuserorderbyidasc"; $result=mysql_query($sql,$con); $userList=''; while($rs=mysql_fetch_array($result)){ $userList[]=$rs; } //循环用户列表 foreach($userListas$user){ echo" <tr> <td> ".$user['id']."</td> <td> ".$user['username']."</td> <td> ".$user['password']."</td> <td> ".$user['sex']."</td> <td> ".$user['age']."</td> <td> ".date("Y-m-d",$user['birthday'])."</td> <td> ".$user['hobby']."</td> <td> ".date("Y-m-d",$user['add_time'])."</td> <td> ".date("Y-m-d",$user['last_login'])."</td> <td> <ahref='addUser.php'>增</a> <ahref='deleteUser.php?id=".$user['id']."');\"> 删</a> <ahref='editUser.php?id=".$user['id']."');\"> 改</a> <ahref='detailUser.php?id=".$user['id']."');\"> 查</a> </td> </tr> "; } ?> </table> </body> </html>
editUser.php
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <title>编辑用户</title> </head> <body> <?php require_once'inc/dbConn.php'; $userId=$_GET['id']; //这是啥东东。。 date_default_timezone_set("PRC"); //读数据。。。 $sql="select*fromuserwhereid=".$userId; $result=mysql_query($sql,$con); $user=mysql_fetch_array($result); ?> <formaction="editDo.php"method="post"> <inputtype="hidden"name="user_id"value="<?phpecho$user['id']?>"/> <tablewidth="444"border="1"align="center"> <tr> <td>用户名</td> <td> <inputtype="text"name="user_name"size="10"value=<?phpecho$user['username']?>/></td> </tr> <tr> <td>密码</td> <td> <inputtype="password"name="password"size="10"value=<?phpecho$user['password']?>/></td> </tr> <tr> <td>性别</td> <td> <inputtype="radio"name="sex"value="男" <?phpif($user[sex]=='1')echo"checked=\"checked\""?>/>男<inputtype="radio"name="sex"value="女"<?phpif($user[sex]=='2')echo"checked=\"checked\""?> />女</td> </tr> <tr> <td>年龄</td> <td> <inputtype="text"name="age"size="3"value=<?phpecho$user['age']?>/></td> </tr> <tr> <td>出生年月</td> <td> <!--1。取得年月日。2。相应的年月日。。。你懂的--> <?php $birday_y=date("Y",$user['birthday']); $birday_m=date("m",$user['birthday']); $birday_d=date("d",$user['birthday']); ?> <selectname="birthday_y"> <optionvalue="1980"<?phpif($birday_y==1980)echo"selected=\"selected\"";?>>1980</option> <optionvalue="1981"<?phpif($birday_y==1981)echo"selected=\"selected\"";?>>1981</option> <optionvalue="1982"<?phpif($birday_y==1982)echo"selected=\"selected\"";?>>1982</option> <optionvalue="1983"<?phpif($birday_y==1983)echo"selected=\"selected\"";?>>1983</option> <optionvalue="1984"<?phpif($birday_y==1984)echo"selected=\"selected\"";?>>1984</option> <optionvalue="1985"<?phpif($birday_y==1985)echo"selected=\"selected\"";?>>1985</option> <optionvalue="1986"<?phpif($birday_y==1986)echo"selected=\"selected\"";?>>1986</option> <optionvalue="1987"<?phpif($birday_y==1987)echo"selected=\"selected\"";?>>1987</option> <optionvalue="1988"<?phpif($birday_y==1988)echo"selected=\"selected\"";?>>1988</option> <optionvalue="1989"<?phpif($birday_y==1989)echo"selected=\"selected\"";?>>1989</option> <optionvalue="1990"<?phpif($birday_y==1990)echo"selected=\"selected\"";?>>1990</option> </select> 年 <selectname="birthday_m"> <optionvalue="1"<?phpif($birday_m==1)echo"selected=\"selected\"";?>>1</option> <optionvalue="2"<?phpif($birday_m==2)echo"selected=\"selected\"";?>>2</option> <optionvalue="3"<?phpif($birday_m==3)echo"selected=\"selected\"";?>>3</option> <optionvalue="4"<?phpif($birday_m==4)echo"selected=\"selected\"";?>>4</option> <optionvalue="5"<?phpif($birday_m==5)echo"selected=\"selected\"";?>>5</option> <optionvalue="6"<?phpif($birday_m==6)echo"selected=\"selected\"";?>>6</option> <optionvalue="7"<?phpif($birday_m==7)echo"selected=\"selected\"";?>>7</option> <optionvalue="8"<?phpif($birday_m==8)echo"selected=\"selected\"";?>>8</option> <optionvalue="9"<?phpif($birday_m==9)echo"selected=\"selected\"";?>>9</option> <optionvalue="10"<?phpif($birday_m==10)echo"selected=\"selected\"";?>>10</option> <optionvalue="11"<?phpif($birday_m==11)echo"selected=\"selected\"";?>>11</option> <optionvalue="12"<?phpif($birday_m==12)echo"selected=\"selected\"";?>>12</option> </select> 月 <selectname="birthday_d"> <optionvalue="1"<?phpif($birday_d==1)echo"selected=\"selected\"";?>>1</option> <optionvalue="2"<?phpif($birday_d==2)echo"selected=\"selected\"";?>>2</option> <optionvalue="3"<?phpif($birday_d==3)echo"selected=\"selected\"";?>>3</option> <optionvalue="4"<?phpif($birday_d==4)echo"selected=\"selected\"";?>>4</option> <optionvalue="5"<?phpif($birday_d==5)echo"selected=\"selected\"";?>>5</option> <optionvalue="6"<?phpif($birday_d==6)echo"selected=\"selected\"";?>>6</option> <optionvalue="7"<?phpif($birday_d==7)echo"selected=\"selected\"";?>>7</option> <optionvalue="8"<?phpif($birday_d==8)echo"selected=\"selected\"";?>>8</option> <optionvalue="9"<?phpif($birday_d==9)echo"selected=\"selected\"";?>>9</option> <optionvalue="10"<?phpif($birday_d==10)echo"selected=\"selected\"";?>>10</option> <optionvalue="11"<?phpif($birday_d==11)echo"selected=\"selected\"";?>>11</option> <optionvalue="12"<?phpif($birday_d==12)echo"selected=\"selected\"";?>>12</option> <optionvalue="13"<?phpif($birday_d==13)echo"selected=\"selected\"";?>>13</option> <optionvalue="14"<?phpif($birday_d==14)echo"selected=\"selected\"";?>>14</option> <optionvalue="15"<?phpif($birday_d==15)echo"selected=\"selected\"";?>>15</option> <optionvalue="16"<?phpif($birday_d==16)echo"selected=\"selected\"";?>>16</option> <optionvalue="17"<?phpif($birday_d==17)echo"selected=\"selected\"";?>>17</option> <optionvalue="18"<?phpif($birday_d==18)echo"selected=\"selected\"";?>>18</option> <optionvalue="19"<?phpif($birday_d==19)echo"selected=\"selected\"";?>>19</option> <optionvalue="20"<?phpif($birday_d==20)echo"selected=\"selected\"";?>>20</option> <optionvalue="21"<?phpif($birday_d==21)echo"selected=\"selected\"";?>>21</option> <optionvalue="22"<?phpif($birday_d==22)echo"selected=\"selected\"";?>>22</option> <optionvalue="23"<?phpif($birday_d==23)echo"selected=\"selected\"";?>>23</option> <optionvalue="24"<?phpif($birday_d==24)echo"selected=\"selected\"";?>>24</option> <optionvalue="25"<?phpif($birday_d==25)echo"selected=\"selected\"";?>>25</option> <optionvalue="26"<?phpif($birday_d==26)echo"selected=\"selected\"";?>>26</option> <optionvalue="27"<?phpif($birday_d==27)echo"selected=\"selected\"";?>>27</option> <optionvalue="28"<?phpif($birday_d==28)echo"selected=\"selected\"";?>>28</option> <optionvalue="29"<?phpif($birday_d==29)echo"selected=\"selected\"";?>>29</option> <optionvalue="30"<?phpif($birday_d==30)echo"selected=\"selected\"";?>>30</option> <optionvalue="31"<?phpif($birday_d==31)echo"selected=\"selected\"";?>>31</option> </select> 日 </td> </tr> <tr> <td>爱好</td> <td> <inputtype="text"name="hobby"size="44"value=<?phpecho$user['hobby']?>/></td> </tr> <tr> <td>个人简介</td> <td> <textareaname="profile"rows="10"cols="30"><?phpecho$user['profile']?></textarea></td> </tr> <tr> <tdcolspan="2"align="center"><inputtype="submit"value="确定修改"/></td> </tr> </table> <p> </p> <p> </p> <p> </p> </form> </body> </html>
editDo.php
<?php require_once'inc/dbConn.php'; //////////////////// ///获取用户信息 //////////////////// $user_id=$_POST['user_id']; $user_name=$_POST['user_name']; $password=$_POST['password']; if($_POST['sex']=='男') { $sex=1; } elseif($_POST['sex']=='女') { $sex=2; } else$sex=3; ; $age=$_POST['age']; $birthday_y=$_POST['birthday_y']; $birthday_m=$_POST['birthday_m']; $birthday_d=$_POST['birthday_d']; $birthday=mktime(0,0,0,$birthday_m,$birthday_d,$birthday_y); $hobby=$_POST['hobby']; $profile=$_POST['profile']; $addTime=mktime(date("h"),date("m"),date("s"),date("m"),date("d"),date("Y")); $lastLoginTime=$addTime; $sql="updateusersetusername='$user_name',password='$password',sex='$sex',age='$age',birthday='$birthday',hobby='$hobby',profile='$profile'whereid='$user_id'"; echo$sql; //执行sql语句 mysql_query($sql,$con); //获取影响的行数 $rows=mysql_affected_rows(); //返回影响行数 //如果影响行数>=1,则判断添加成功,否则失败 if($rows>=1) { alert("编辑成功"); href("userListt.php"); }else{ alert("编辑失败"); //href("addUser.php"); } functionalert($title){ echo"<scripttype='text/javascript'>alert('$title');</script>"; } functionhref($url){ echo"<scripttype='text/javascript'>window.location.href='$url'</script>"; } ?>
detailUser.php
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <title>查看用户明细</title> </head> <body> <?php require_once'inc/dbConn.php'; $userId=$_GET['id']; //这是啥东东。。 date_default_timezone_set("PRC"); //读数据。。。 $sql="select*fromuserwhereid=".$userId; $result=mysql_query($sql,$con); $user=mysql_fetch_array($result); ?> <tablewidth="444"border="1"align="center"> <tr> <td>用户ID</td> <td> <?phpecho$userId?></td> </tr> <tr> <td>用户名</td> <td> <?phpecho$user['username']?></td> </tr> <tr> <td>密码</td> <td> <?phpecho$user['password']?></td> </tr> <tr> <td>性别</td> <td> <?phpif($user[sex]=='1')echo"男";elseif($user[sex]=='2')echo"女";else"保密";?> </td> </tr> <tr> <td>年龄</td> <td> <?phpecho$user['age']?></td> </tr> <tr> <td>出生年月</td> <td> <!--1。取得年月日。2。相应的年月日。。。你懂的--> <?php $birday_y=date("Y",$user['birthday']);echo$birday_y."年"; $birday_m=date("m",$user['birthday']);echo$birday_m."月"; $birday_d=date("d",$user['birthday']);echo$birday_d."日"; ?> </td> </tr> <tr> <td>爱好</td> <td> <?phpecho$user['hobby']?></td> </tr> <tr> <td>个人简介</td> <!--亦可框起<td> <textareaname="profile"rows="10"cols="30"readonly><?phpecho$user['profile']?></textarea></td>--> <td> <?phpecho$user['profile']?></td> </tr> <tr> <tdcolspan="2"align="center"><ahref="userListt.php">返回用户列表</a></td> </tr> </table> <p> </p> <p> </p> <p> </p> </body> </html>
deleteUser.php
<?php //包含数据库文件 require_once'inc/dbConn.php'; //获取删除的id $id=$_GET['id']; $row=delete($id,$con); if($row>=1){ alert("删除成功"); }else{ alert("删除失败"); } //跳转到用户列表页面 href("userListt.php"); functiondelete($id,$con){ $sql="deletefromuserwhereid='$id'"; //执行删除 mysql_query($sql,$con); //获取影响的行数 $rows=mysql_affected_rows(); //返回影响行数 return$rows; } functionalert($title){ echo"<scripttype='text/javascript'>alert('$title');</script>"; } functionhref($url){ echo"<scripttype='text/javascript'>window.location.href='$url'</script>"; } ?>
addUser.php
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <title>新增用户</title> </head> <body> <formaction="addDo.php"method="post"> <inputtype="hidden"name="user_id"value=""/> <tablewidth="444"border="1"align="center"> <tr> <td>用户名</td> <td> <inputtype="text"name="user_name"size="10"/></td> </tr> <tr> <td>密码</td> <td> <inputtype="password"name="password"size="10"/></td> </tr> <tr> <td>性别</td> <td> <inputtype="radio"name="sex"value="男"checked="checked"/>男<inputtype="radio"name="sex"value="女"/>女</td> </tr> <tr> <td>年龄</td> <td> <inputtype="text"name="age"size="3"/></td> </tr> <tr> <td>出生年月</td> <td> <selectname="birthday_y"> <optionvalue="1980">1980</option> <optionvalue="1981">1981</option> <optionvalue="1982">1982</option> <optionvalue="1983">1983</option> <optionvalue="1984">1984</option> <optionvalue="1985">1985</option> <optionvalue="1986">1986</option> <optionvalue="1987">1987</option> <optionvalue="1988">1988</option> <optionvalue="1989">1989</option> <optionvalue="1990">1990</option> </select> 年 <selectname="birthday_m"> <optionvalue="1">1</option> <optionvalue="2">2</option> <optionvalue="3">3</option> <optionvalue="4">4</option> <optionvalue="5">5</option> <optionvalue="6">6</option> <optionvalue="7">7</option> <optionvalue="8">8</option> <optionvalue="9">9</option> <optionvalue="10">10</option> <optionvalue="11">11</option> <optionvalue="12">12</option> </select> 月 <selectname="birthday_d"> <optionvalue="1">1</option> <optionvalue="2">2</option> <optionvalue="3">3</option> <optionvalue="4">4</option> <optionvalue="5">5</option> <optionvalue="6">6</option> <optionvalue="7">7</option> <optionvalue="8">8</option> <optionvalue="9">9</option> <optionvalue="10">10</option> <optionvalue="11">11</option> <optionvalue="12">12</option> <optionvalue="13">13</option> <optionvalue="14">14</option> <optionvalue="15">15</option> <optionvalue="16">16</option> <optionvalue="17">17</option> <optionvalue="18">18</option> <optionvalue="19">19</option> <optionvalue="20">20</option> <optionvalue="21">21</option> <optionvalue="22">22</option> <optionvalue="23">23</option> <optionvalue="24">24</option> <optionvalue="25">25</option> <optionvalue="26">26</option> <optionvalue="27">27</option> <optionvalue="28">28</option> <optionvalue="29">29</option> <optionvalue="30">30</option> <optionvalue="31">31</option> </select> 日 </td> </tr> <tr> <td>爱好</td> <td> <inputtype="text"name="hobby"size="44"/></td> </tr> <tr> <td>个人简介</td> <td> <textareaname="profile"rows="10"cols="30"></textarea></td> </tr> <tr> <tdcolspan="2"align="center"><inputtype="submit"value="增加用户"/></td> </tr> </table> <p> </p> <p> </p> <p> </p> </form> </body> </html>
addDo.php
<?php require_once'inc/dbConn.php'; //////////////////// ///获取用户信息 //////////////////// $user_id=$_POST['user_id']; $user_name=$_POST['user_name']; echo$user_name; $password=$_POST['password']; if($_POST['sex']=='男') { $sex=1; } elseif($_POST['sex']=='女') { $sex=2; } else$sex=3; ; $age=$_POST['age']; $birthday_y=$_POST['birthday_y']; $birthday_m=$_POST['birthday_m']; $birthday_d=$_POST['birthday_d']; $birthday=mktime(0,0,0,$birthday_m,$birthday_d,$birthday_y); $hobby=$_POST['hobby']; $profile=$_POST['profile']; $addTime=mktime(date("h"),date("m"),date("s"),date("m"),date("d"),date("Y")); $lastLoginTime=$addTime; $sql="insertintouser(username,password,sex,age,birthday,hobby,profile,add_time,last_login)". "values('$user_name','$password','$sex','$age','$birthday','$hobby','$profile','$addTime','$lastLoginTime')"; echo$sql; //执行sql语句 mysql_query($sql,$con); //获取影响的行数 $rows=mysql_affected_rows(); //返回影响行数 //如果影响行数>=1,则判断添加成功,否则失败 if($rows>=1){ alert("添加成功"); href("userListt.php"); }else{ alert("添加失败"); //href("addUser.php"); } functionalert($title){ echo"<scripttype='text/javascript'>alert('$title');</script>"; } functionhref($url){ echo"<scripttype='text/javascript'>window.location.href='$url'</script>"; } ?>