首頁  >  文章  >  後端開發  >  php連線資料庫實作使用者資料的增刪改查實例

php連線資料庫實作使用者資料的增刪改查實例

黄舟
黄舟原創
2017-09-02 10:04:193913瀏覽

下面小編就為大家帶來一篇使用PHP連線資料庫_實現使用者資料的增刪改查的整體操作範例。小編覺得蠻不錯的,現在就分享給大家,也給大家做個參考。一起跟著小編過來看看吧

main頁面(主頁面)


<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>代号</td>
<td>姓名</td>
<td>性别</td>
<td>民族</td>
<td>生日</td>
<td>操作</td>
</tr>

<?php
$db = new MySQLi("localhost","root","","mydb");
if(mysqli_connect_error()){

  die("连接失败");

} 
$sql = "select * from Info";
$result = $db->query($sql);
$attr = $result->fetch_all();
foreach($attr as $v)
{
 $sex = $v[2]? &#39;男&#39;:&#39;女&#39;;  //三元运算符判断性别
 $sql = "select Name from Nation where Code =&#39;$v[3]&#39;";
 $result = $db ->query($sql);
 $attr = $result->fetch_assoc();
 echo "<tr>
 <td>{$v[0]}</td>
 <td>{$v[1]}</td>
 <td>{$sex}</td>
 <td>{$attr[&#39;Name&#39;]}</td>
 <td>{$v[4]}</td>
 <td>
  <a href=&#39;Delete.php?code={$v[0]}&#39;>删除</a>
  <a href=&#39;Update.php?code={$v[0]}&#39;>修改</a>
 </td>
 </tr>";
}
?>

</table>
<p>

<a href="Add.php" rel="external nofollow" >添加数据</a>

</p>

Add(新增資料頁面)


<h1>添加数据</h1>
<form action="AddChuLi.php" method="post">
 <p>代号:<input type="text" name="code" /></p>
 <p>姓名:<input type="text" name="name" /></p>
 <p>性别:
   <input type="radio" value="男" name="sex" />男
   <input type="radio" value="女" name="sex" />女
 </p>
 <p>民族:
   <select name="nation">
   <?php
   $db = new MySQLi("localhost","root","","mydb");
  if(mysqli_connect_error()){

  die("连接失败");

  }
   $sql = "select * from Nation";
   $r = $db->query($sql);
   $att = $r->fetch_all();
   foreach($att as $v)
   {
    echo "<option value=&#39;{$v[0]}&#39;>{$v[1]}</option>";
   }
   ?>
   </select>
 </p>
 <p>生日:<input type="text" name="birthday" /></p>
 <p><input type="submit" value="添加数据" /></p>
</form>

AddChuLi頁面(新增資料處理頁面)


<?php
$code = $_POST["code"];
$name = $_POST["name"];
$sex = $_POST["sex"];
$s = 1;
if($sex =="女")
{
 $s=0;
}
$nation = $_POST["nation"];
$birthday = $_POST["birthday"];
$db = new MySQLi("localhost","root","","mydb");
if(mysqli_connect_error()){

  die("连接失败");

} 

$sql = "insert into Info values(&#39;{$code}&#39;,&#39;{$name}&#39;,&#39;{$s}&#39;,&#39;{$nation}&#39;,&#39;{$birthday}&#39;)";  //添加数据语句
$result = $db->query($sql);
if($result)
{
 header("location:main.php");  //php跳转页面方式
}
else
{
 echo "添加失败!";
}

DeleteChuLi頁面(刪除資料處理頁面)


<?php
$code = $_GET["code"];
$db = new MySQLi("localhost","root","","mydb");
if(mysqli_connect_error()){

  die("连接失败");

} 

$sql = "delete from Info where Code =&#39;{$code}&#39;";  //删除语句
$r = $db->query($sql);
if($r)
{
 header("location:main.php");
}
else
{
 echo "删除失败!";
}

Update頁面(修改資料頁面)


<h1>修改数据</h1>
<?php
$code = $_GET["code"];
$db = new MySQLi("localhost","root","","mydb");
if(mysqli_connect_error()){

  die("连接失败");

} 

$sql1 = "select * from Info where Code=&#39;{$code}&#39;";
$r1 = $db->query($sql1);
$att1 = $r1->fetch_row();
?>


<form action="UpdateChuLi.php" method="post">
 <p>代号:<input type="hidden" name="code" value="<?php echo $att1[0] ?>" /></p>
 <p>姓名:<input type="text" name="name" value="<?php echo $att1[1] ?>" /></p>
 <p>性别:
   <input type="radio" value="男" name="sex" <?php echo $att1[2] ? "checked=&#39;checked&#39;" : ""; ?> />男
   <input type="radio" value="女" name="sex" <?php echo $att1[2] ? "" : "checked=&#39;checked&#39;"; ?> />女
 </p>
 <p>民族:
   <select name="nation">
   <?php
   $db = new MySQLi("localhost","root","","mydb");
   if(mysqli_connect_error()){

  die("连接失败");

  }
   $sql = "select * from Nation";  //查询nation一组数据
   $r = $db->query($sql);
   $att = $r->fetch_all();
   foreach($att as $v)
   {
    if($att1[3]==$v[0])
    {
     echo "<option value=&#39;{$v[0]}&#39; selected=&#39;selectec&#39; >{$v[1]}</option>";  
    }
    else
    {
     echo "<option value=&#39;{$v[0]}&#39;>{$v[1]}</option>";
    }
    
   }
   ?>
   </select>
 </p>
 <p>生日:<input type="text" name="birthday" value="<?php echo $att1[4] ?>"/></p>
 <p><input type="submit" value="修改数据" /></p>
</form>

ateChuLi頁面(修改數據處理頁面)


<?php
$code = $_POST["code"];
$name = $_POST["name"];
$sex = $_POST["sex"];
$s=1;
if($sex=="女")
{
 $s=0;
}
$nation = $_POST["nation"];
$birthday = $_POST["birthday"];
$db = new MySQLi("localhost","root","","mydb");
if(mysqli_connect_error()){

  die("连接失败");

} 

$sql = "update Info set Name=&#39;{$name}&#39;,Sex={$s},Nation=&#39;{$nation}&#39;,Birthday=&#39;{$birthday}&#39; where Code=&#39;{$code}&#39;";  //修改数据语句
$r = $db->query($sql);
if($r)
{
 header("location:main.php");
}
else
{
 echo "修改失败!";
}

以上是php連線資料庫實作使用者資料的增刪改查實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn