首頁 > 後端開發 > php教程 > php資料庫連線及使用者管理

php資料庫連線及使用者管理

墨辰丷
發布: 2023-03-29 22:48:01
原創
2699 人瀏覽過

本篇主要介紹php資料庫連線及使用者管理,有興趣的朋友參考下,希望對大家有幫助。文件列表。 。文件內容。 。
程式碼如下:

<?php
// 创建数据库连接
$con = mysql_connect("localhost",&#39;root&#39;,&#39;&#39;) or die(&#39;error:&#39;.mysql_error());
mysql_select_db(&#39;hyxx&#39;,$con) or die(&#39;error:&#39;.mysql_error());
mysql_query(&#39;set NAMES utf8&#39;);
?>
登入後複製

userListt.php

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>会员信息列表</title>
</head>
  <body>
    <table border="1" cellspacing="0" cellpadding="0" id="userList" align="center">
   <tr align="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 &#39;inc/dbConn.php&#39;;
//这是啥东东。。
date_default_timezone_set("PRC");
//读数据。。。
  $sql = "select * from user order by id asc";
  $result = mysql_query($sql,$con);
  $userList = &#39;&#39;;
  while($rs = mysql_fetch_array($result)){
        $userList[] = $rs;
    }
          // 循环用户列表
        foreach ($userList as $user){
          echo "
             <tr>
              <td> ".$user[&#39;id&#39;]."</td>
              <td> ".$user[&#39;username&#39;]."</td>
              <td> ".$user[&#39;password&#39;]."</td>
              <td> ".$user[&#39;sex&#39;]."</td>
              <td> ".$user[&#39;age&#39;]."</td>
              <td> ".date("Y-m-d",$user[&#39;birthday&#39;])."</td>
              <td> ".$user[&#39;hobby&#39;]."</td>
              <td> ".date("Y-m-d",$user[&#39;add_time&#39;])."</td>
              <td> ".date("Y-m-d",$user[&#39;last_login&#39;])."</td>
              <td> <a href=&#39;addUser.php&#39;>增</a> 
              <a href=&#39;deleteUser.php?id=".$user[&#39;id&#39;]."&#39;);\">&nbsp删</a>
              <a href=&#39;editUser.php?id=".$user[&#39;id&#39;]."&#39;);\">&nbsp改</a>
              <a href=&#39;detailUser.php?id=".$user[&#39;id&#39;]."&#39;);\">&nbsp查</a>                    
              </td>
             </tr>
          ";
        }
?>
  </table>
  </body>
</html>
登入後複製

editUser.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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>编辑用户</title>
</head>
<body>
<?php 
require_once &#39;inc/dbConn.php&#39;;
$userId=$_GET[&#39;id&#39;];
//这是啥东东。。
date_default_timezone_set("PRC");
//读数据。。。
  $sql = "select * from user where id=".$userId;
  $result = mysql_query($sql,$con);
  $user = mysql_fetch_array($result);
?>
<form action="editDo.php" method="post">
    <input type="hidden" name="user_id" value="<?php echo $user[&#39;id&#39;]?>"/>
    <table width="444" border="1" align="center">
       <tr>
        <td>用户名 </td>
        <td> <input type="text" name="user_name" size="10" value=<?php echo $user[&#39;username&#39;] ?> /></td>
       </tr>
       <tr>
        <td>密码</td>
        <td> <input type="password" name="password" size="10" value=<?php echo $user[&#39;password&#39;] ?> /></td>
       </tr>
       <tr>
        <td>性别</td> 
        <td> <input type="radio" name="sex" value="男"
         <?php if($user[sex]==&#39;1&#39;) echo "checked=\"checked\"" ?> /> 男<input type="radio" name="sex" value="女" <?php if($user[sex]==&#39;2&#39;) echo "checked=\"checked\"" ?>
         /> 女 </td>
       </tr>
       <tr>
        <td>年龄</td>
        <td> <input type="text" name="age" size="3" value=<?php echo $user[&#39;age&#39;] ?> /></td>
       </tr>
       <tr>
        <td>出生年月</td>
        <td> 
        <!-- 1。取得年月日。2。相应的年月日。。。你懂的 -->
        <?php
        $birday_y=date("Y",$user[&#39;birthday&#39;]);
        $birday_m=date("m",$user[&#39;birthday&#39;]);
        $birday_d=date("d",$user[&#39;birthday&#39;]);
        ?>
          <select name="birthday_y">
          <option value="1980" <?php if($birday_y==1980) echo "selected=\"selected\""; ?> >1980</option>
          <option value="1981" <?php if($birday_y==1981) echo "selected=\"selected\""; ?> >1981</option>
          <option value="1982" <?php if($birday_y==1982) echo "selected=\"selected\""; ?> >1982</option>
          <option value="1983" <?php if($birday_y==1983) echo "selected=\"selected\""; ?> >1983</option>
          <option value="1984" <?php if($birday_y==1984) echo "selected=\"selected\""; ?> >1984</option>
          <option value="1985" <?php if($birday_y==1985) echo "selected=\"selected\""; ?> >1985</option>
          <option value="1986" <?php if($birday_y==1986) echo "selected=\"selected\""; ?> >1986</option>
          <option value="1987" <?php if($birday_y==1987) echo "selected=\"selected\""; ?> >1987</option>
          <option value="1988" <?php if($birday_y==1988) echo "selected=\"selected\""; ?> >1988</option>
          <option value="1989" <?php if($birday_y==1989) echo "selected=\"selected\""; ?> >1989</option>
          <option value="1990" <?php if($birday_y==1990) echo "selected=\"selected\""; ?> >1990</option>
          </select>
          年
          <select name="birthday_m">
          <option value="1" <?php if($birday_m==1) echo "selected=\"selected\""; ?> >1</option>
          <option value="2" <?php if($birday_m==2) echo "selected=\"selected\""; ?> >2</option>
          <option value="3" <?php if($birday_m==3) echo "selected=\"selected\""; ?> >3</option>
          <option value="4" <?php if($birday_m==4) echo "selected=\"selected\""; ?> >4</option>
          <option value="5" <?php if($birday_m==5) echo "selected=\"selected\""; ?> >5</option>
          <option value="6" <?php if($birday_m==6) echo "selected=\"selected\""; ?> >6</option>
          <option value="7" <?php if($birday_m==7) echo "selected=\"selected\""; ?> >7</option>
          <option value="8" <?php if($birday_m==8) echo "selected=\"selected\""; ?> >8</option>
          <option value="9" <?php if($birday_m==9) echo "selected=\"selected\""; ?> >9</option>
          <option value="10" <?php if($birday_m==10) echo "selected=\"selected\""; ?> >10</option>
          <option value="11" <?php if($birday_m==11) echo "selected=\"selected\""; ?> >11</option>
          <option value="12" <?php if($birday_m==12) echo "selected=\"selected\""; ?> >12</option>
          </select>
          月 
          <select name="birthday_d">
          <option value="1" <?php if($birday_d==1) echo "selected=\"selected\""; ?> >1</option>
          <option value="2" <?php if($birday_d==2) echo "selected=\"selected\""; ?> >2</option>
          <option value="3" <?php if($birday_d==3) echo "selected=\"selected\""; ?> >3</option>
          <option value="4" <?php if($birday_d==4) echo "selected=\"selected\""; ?> >4</option>
          <option value="5" <?php if($birday_d==5) echo "selected=\"selected\""; ?> >5</option>
          <option value="6" <?php if($birday_d==6) echo "selected=\"selected\""; ?> >6</option>
          <option value="7" <?php if($birday_d==7) echo "selected=\"selected\""; ?> >7</option>
          <option value="8" <?php if($birday_d==8) echo "selected=\"selected\""; ?> >8</option>
          <option value="9" <?php if($birday_d==9) echo "selected=\"selected\""; ?> >9</option>
          <option value="10" <?php if($birday_d==10) echo "selected=\"selected\""; ?> >10</option>
          <option value="11" <?php if($birday_d==11) echo "selected=\"selected\""; ?> >11</option>
          <option value="12" <?php if($birday_d==12) echo "selected=\"selected\""; ?> >12</option>
          <option value="13" <?php if($birday_d==13) echo "selected=\"selected\""; ?> >13</option>
          <option value="14" <?php if($birday_d==14) echo "selected=\"selected\""; ?> >14</option>
          <option value="15" <?php if($birday_d==15) echo "selected=\"selected\""; ?> >15</option>
          <option value="16" <?php if($birday_d==16) echo "selected=\"selected\""; ?> >16</option>
          <option value="17" <?php if($birday_d==17) echo "selected=\"selected\""; ?> >17</option>
          <option value="18" <?php if($birday_d==18) echo "selected=\"selected\""; ?> >18</option>
          <option value="19" <?php if($birday_d==19) echo "selected=\"selected\""; ?> >19</option>
          <option value="20" <?php if($birday_d==20) echo "selected=\"selected\""; ?> >20</option>
          <option value="21" <?php if($birday_d==21) echo "selected=\"selected\""; ?> >21</option>
          <option value="22" <?php if($birday_d==22) echo "selected=\"selected\""; ?> >22</option>
          <option value="23" <?php if($birday_d==23) echo "selected=\"selected\""; ?> >23</option>
          <option value="24" <?php if($birday_d==24) echo "selected=\"selected\""; ?> >24</option>
          <option value="25" <?php if($birday_d==25) echo "selected=\"selected\""; ?> >25</option>
          <option value="26" <?php if($birday_d==26) echo "selected=\"selected\""; ?> >26</option>
          <option value="27" <?php if($birday_d==27) echo "selected=\"selected\""; ?> >27</option>
          <option value="28" <?php if($birday_d==28) echo "selected=\"selected\""; ?> >28</option>
          <option value="29" <?php if($birday_d==29) echo "selected=\"selected\""; ?> >29</option>
          <option value="30" <?php if($birday_d==30) echo "selected=\"selected\""; ?> >30</option>
          <option value="31" <?php if($birday_d==31) echo "selected=\"selected\""; ?> >31</option>
          </select>
          日
        </td>
       </tr>
       <tr>
        <td>爱好</td>
        <td> <input type="text" name="hobby" size="44" value=<?php echo $user[&#39;hobby&#39;] ?> /></td>
       </tr>
       <tr>
        <td>个人简介</td>
        <td> <textarea name="profile" rows="10" cols="30" ><?php echo $user[&#39;profile&#39;] ?> </textarea> </td>
       </tr>
       <tr>
          <td colspan="2" align="center"><input type="submit" value="确定修改" /></td>
       </tr>
 </table>
      <p> </p>
      <p> </p>
      <p> </p>
</form>
</body>
</html>
登入後複製

editDo.php

<?php
require_once &#39;inc/dbConn.php&#39;;
////////////////////
///获取用户信息
////////////////////
$user_id = $_POST[&#39;user_id&#39;];
$user_name = $_POST[&#39;user_name&#39;];
$password= $_POST[&#39;password&#39;];
if($_POST[&#39;sex&#39;]==&#39;男&#39;)
{
  $sex=1;
}
else if($_POST[&#39;sex&#39;]==&#39;女&#39;)
{
    $sex=2;
}
else $sex=3;    
;
$age = $_POST[&#39;age&#39;];
$birthday_y = $_POST[&#39;birthday_y&#39;];
$birthday_m = $_POST[&#39;birthday_m&#39;];
$birthday_d = $_POST[&#39;birthday_d&#39;];
$birthday=mktime(0,0,0,$birthday_m,$birthday_d,$birthday_y);
$hobby = $_POST[&#39;hobby&#39;];
$profile = $_POST[&#39;profile&#39;];
$addTime=mktime(date("h"),date("m"),date("s"),date("m"),date("d"),date("Y"));
$lastLoginTime=$addTime;
  $sql = "update user set username=&#39;$user_name&#39;,password=&#39;$password&#39;,sex=&#39;$sex&#39;,age=&#39;$age&#39;,birthday=&#39;$birthday&#39;,hobby=&#39;$hobby&#39;,profile=&#39;$profile&#39; where id=&#39;$user_id&#39;";
  echo $sql;
  // 执行sql语句
  mysql_query($sql,$con);
  // 获取影响的行数
  $rows = mysql_affected_rows();
  // 返回影响行数
  // 如果影响行数>=1,则判断添加成功,否则失败
  if($rows >= 1)
  {
    alert("编辑成功");
    href("userListt.php");
  }else{
    alert("编辑失败");
//   href("addUser.php");
    }
function alert($title){
  echo "<script type=&#39;text/javascript&#39;>alert(&#39;$title&#39;);</script>";
}
function href($url){
  echo "<script type=&#39;text/javascript&#39;>window.location.href=&#39;$url&#39;</script>";
}
?>
登入後複製

detailUser.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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>查看用户明细</title>
</head>
<body>
<?php 
require_once &#39;inc/dbConn.php&#39;;
$userId=$_GET[&#39;id&#39;];
//这是啥东东。。
date_default_timezone_set("PRC");
//读数据。。。
  $sql = "select * from user where id=".$userId;
  $result = mysql_query($sql,$con);
  $user = mysql_fetch_array($result);
?>
    <table width="444" border="1" align="center">
       <tr>
        <td>用户ID </td>
        <td> <?php echo $userId ?> </td>
       </tr>
       <tr>
        <td>用户名 </td>
        <td> <?php echo $user[&#39;username&#39;] ?> </td>
       </tr>
       <tr>
        <td>密码</td>
        <td> <?php echo $user[&#39;password&#39;] ?> </td>
       </tr>
       <tr>
        <td>性别</td> 
        <td> <?php if($user[sex]==&#39;1&#39;) echo "男"; else if($user[sex]==&#39;2&#39;) echo "女"; else "保密"; ?>
        </td>
       </tr>
       <tr>
        <td>年龄</td>
        <td> <?php echo $user[&#39;age&#39;] ?> </td>
       </tr>
       <tr>
        <td>出生年月</td>
        <td> 
        <!-- 1。取得年月日。2。相应的年月日。。。你懂的 -->
        <?php
        $birday_y=date("Y",$user[&#39;birthday&#39;]);echo $birday_y."年";
        $birday_m=date("m",$user[&#39;birthday&#39;]);echo $birday_m."月";
        $birday_d=date("d",$user[&#39;birthday&#39;]);echo $birday_d."日";
        ?>
        </td>
       </tr>
       <tr>
        <td>爱好</td>
        <td> <?php echo $user[&#39;hobby&#39;] ?> </td>
       </tr>
       <tr>
        <td>个人简介</td>
        <!--亦可框起 <td> <textarea name="profile" rows="10" cols="30" readonly><?php echo $user[&#39;profile&#39;] ?> </textarea> </td> -->
        <td> <?php echo $user[&#39;profile&#39;] ?> </td>
       </tr>
       <tr>
          <td colspan="2" align="center"><a href="userListt.php" >返回用户列表</a></td>
       </tr>
 </table>
      <p> </p>
      <p> </p>
      <p> </p>
</body>
</html>
登入後複製

deleteUser.php

<?php
// 包含数据库文件
require_once &#39;inc/dbConn.php&#39;;
// 获取删除的id
$id = $_GET[&#39;id&#39;];
$row = delete($id,$con);
if($row >=1){
  alert("删除成功");
}else{
  alert("删除失败");
}
// 跳转到用户列表页面
href("userListt.php");
function delete($id,$con){
  $sql = "delete from user where id=&#39;$id&#39;";
  // 执行删除
  mysql_query($sql,$con);
  // 获取影响的行数
  $rows = mysql_affected_rows();
  // 返回影响行数
  return $rows;
}
function alert($title){
  echo "<script type=&#39;text/javascript&#39;>alert(&#39;$title&#39;);</script>";
}
function href($url){
  echo "<script type=&#39;text/javascript&#39;>window.location.href=&#39;$url&#39;</script>";
}
?>
登入後複製

addUser.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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新增用户</title>
</head>
<body>
<form action="addDo.php" method="post">
      <input type="hidden" name="user_id" value=" "/>
      <table width="444" border="1" align="center">
       <tr>
        <td>用户名 </td>
        <td> <input type="text" name="user_name" size="10" /></td>
       </tr>
       <tr>
        <td>密码</td>
        <td> <input type="password" name="password" size="10" /></td>
       </tr>
       <tr>
        <td>性别</td>
        <td> <input type="radio" name="sex" value="男" checked="checked" /> 男<input type="radio" name="sex" value="女" /> 女 </td>
       </tr>
       <tr>
        <td>年龄</td>
        <td> <input type="text" name="age" size="3" /></td>
       </tr>
       <tr>
        <td>出生年月</td>
        <td> 
          <select name="birthday_y">
          <option value="1980">1980</option>
          <option value="1981">1981</option>
          <option value="1982">1982</option>
          <option value="1983">1983</option>
          <option value="1984">1984</option>
          <option value="1985">1985</option>
          <option value="1986">1986</option>
          <option value="1987">1987</option>
          <option value="1988">1988</option>
          <option value="1989">1989</option>
          <option value="1990">1990</option>
          </select>
          年
          <select name="birthday_m">
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          <option value="4">4</option>
          <option value="5">5</option>
          <option value="6">6</option>
          <option value="7">7</option>
          <option value="8">8</option>
          <option value="9">9</option>
          <option value="10">10</option>
          <option value="11">11</option>
          <option value="12">12</option>
          </select>
          月
          <select name="birthday_d">
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          <option value="4">4</option>
          <option value="5">5</option>
          <option value="6">6</option>
          <option value="7">7</option>
          <option value="8">8</option>
          <option value="9">9</option>
          <option value="10">10</option>
          <option value="11">11</option>
          <option value="12">12</option>
          <option value="13">13</option>
          <option value="14">14</option>
          <option value="15">15</option>
          <option value="16">16</option>
          <option value="17">17</option>
          <option value="18">18</option>
          <option value="19">19</option>
          <option value="20">20</option>
          <option value="21">21</option>
          <option value="22">22</option>
          <option value="23">23</option>
          <option value="24">24</option>
          <option value="25">25</option>
          <option value="26">26</option>
          <option value="27">27</option>
          <option value="28">28</option>
          <option value="29">29</option>
          <option value="30">30</option>
          <option value="31">31</option>
          </select>
          日
        </td>
       </tr>
       <tr>
        <td>爱好</td>
        <td> <input type="text" name="hobby" size="44" /></td>
       </tr>
       <tr>
        <td>个人简介</td>
        <td> <textarea name="profile" rows="10" cols="30"></textarea></td>
       </tr>
       <tr>
          <td colspan="2" align="center"><input type="submit" value="增加用户" /></td>
  </tr>
 </table>
      <p> </p>
      <p> </p>
      <p> </p>
</form>
</body>
</html>
登入後複製

addDo.php

<?php 
require_once &#39;inc/dbConn.php&#39;;
////////////////////
///获取用户信息
////////////////////
$user_id = $_POST[&#39;user_id&#39;];
$user_name = $_POST[&#39;user_name&#39;];
echo $user_name;
$password= $_POST[&#39;password&#39;];
if($_POST[&#39;sex&#39;]==&#39;男&#39;)
{
  $sex=1;
}
else if($_POST[&#39;sex&#39;]==&#39;女&#39;)
{
    $sex=2;
}
else $sex=3;    
;
$age = $_POST[&#39;age&#39;];
$birthday_y = $_POST[&#39;birthday_y&#39;];
$birthday_m = $_POST[&#39;birthday_m&#39;];
$birthday_d = $_POST[&#39;birthday_d&#39;];
$birthday=mktime(0,0,0,$birthday_m,$birthday_d,$birthday_y);
$hobby = $_POST[&#39;hobby&#39;];
$profile = $_POST[&#39;profile&#39;];
$addTime=mktime(date("h"),date("m"),date("s"),date("m"),date("d"),date("Y"));
$lastLoginTime=$addTime;
  $sql = "insert into user (username,password,sex,age,birthday,hobby,profile,add_time,last_login) ".
  "values(&#39;$user_name&#39;,&#39;$password&#39;,&#39;$sex&#39;,&#39;$age&#39;,&#39;$birthday&#39;,&#39;$hobby&#39;,&#39;$profile&#39;,&#39;$addTime&#39;,&#39;$lastLoginTime&#39;)";
  echo $sql;
  // 执行sql语句
  mysql_query($sql,$con);
  // 获取影响的行数
  $rows = mysql_affected_rows();
  // 返回影响行数
  // 如果影响行数>=1,则判断添加成功,否则失败
  if($rows >= 1){
    alert("添加成功");
    href("userListt.php");
  }else{
    alert("添加失败");
//   href("addUser.php");
    }
function alert($title){
  echo "<script type=&#39;text/javascript&#39;>alert(&#39;$title&#39;);</script>";
}
function href($url){
  echo "<script type=&#39;text/javascript&#39;>window.location.href=&#39;$url&#39;</script>";
}
?>
登入後複製

總結:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。

相關推薦:

php計算目錄檔案大小的函數

#php自訂分頁類別的原理

#PHP匯出word的方法

以上是php資料庫連線及使用者管理的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板