欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用PHP连接数据库_实现用户数据的增删改查的整体操作示例

程序员文章站 2022-06-15 23:25:53
main页面(主页面) ...

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]? '男':'女';  //三元运算符判断性别
 $sql = "select name from nation where code ='$v[3]'";
 $result = $db ->query($sql);
 $attr = $result->fetch_assoc();
 echo "<tr>
 <td>{$v[0]}</td>
 <td>{$v[1]}</td>
 <td>{$sex}</td>
 <td>{$attr['name']}</td>
 <td>{$v[4]}</td>
 <td>
  <a href='delete.php?code={$v[0]}'>删除</a>
  <a href='update.php?code={$v[0]}'>修改</a>
 </td>
 </tr>";
}
?>

</table>
<div>

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

</div>

add(添加数据页面)

<h1>添加数据</h1>
<form action="addchuli.php" method="post">
 <div>代号:<input type="text" name="code" /></div>
 <div>姓名:<input type="text" name="name" /></div>
 <div>性别:
   <input type="radio" value="男" name="sex" />男
   <input type="radio" value="女" name="sex" />女
 </div>
 <div>民族:
   <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='{$v[0]}'>{$v[1]}</option>";
   }
   ?>
   </select>
 </div>
 <div>生日:<input type="text" name="birthday" /></div>
 <div><input type="submit" value="添加数据" /></div>
</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('{$code}','{$name}','{$s}','{$nation}','{$birthday}')";  //添加数据语句
$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 ='{$code}'";  //删除语句
$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='{$code}'";
$r1 = $db->query($sql1);
$att1 = $r1->fetch_row();
?>


<form action="updatechuli.php" method="post">
 <div>代号:<input type="hidden" name="code" value="<?php echo $att1[0] ?>" /></div>
 <div>姓名:<input type="text" name="name" value="<?php echo $att1[1] ?>" /></div>
 <div>性别:
   <input type="radio" value="男" name="sex" <?php echo $att1[2] ? "checked='checked'" : ""; ?> />男
   <input type="radio" value="女" name="sex" <?php echo $att1[2] ? "" : "checked='checked'"; ?> />女
 </div>
 <div>民族:
   <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='{$v[0]}' selected='selectec' >{$v[1]}</option>";  
    }
    else
    {
     echo "<option value='{$v[0]}'>{$v[1]}</option>";
    }
    
   }
   ?>
   </select>
 </div>
 <div>生日:<input type="text" name="birthday" value="<?php echo $att1[4] ?>"/></div>
 <div><input type="submit" value="修改数据" /></div>
</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='{$name}',sex={$s},nation='{$nation}',birthday='{$birthday}' where code='{$code}'";  //修改数据语句
$r = $db->query($sql);
if($r)
{
 header("location:main.php");
}
else
{
 echo "修改失败!";
}

以上这篇使用php连接数据库_实现用户数据的增删改查的整体操作示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。