php实现数据库的增删改查

By admin in 计算机教程 on 2019年7月28日

main页面(主页面)

1.查询:

<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>

数据的显示,这里就可以嵌入php来进行数据的输出

Add(添加数据页面)

<!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>
<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","12345678","heiheihei");
//连接数据库
$sql = "select * from student ";
//写sql语句
$r = $db->query($sql);
//执行sql语句返回给r
if($r)//条件
{
  while ($attr = $r->fetch_row())
  {
    $ssex = "";
     if($attr[2])
     {
       $ssex = "男";
     }
     else
       {
         $ssex = "女";
       }
    echo
    "
 <tr>
    <td>{$attr[0]}</td>
    <td>{$attr[1]}</td>
    <td>{$ssex}</td>
    <td>{$attr[3]}</td>
    <td>{$attr[4]}</td>
//添加一个单击事件,防止不小心删掉
<td><a onclick=\"return confirm('确定要删除吗???')" 
href='shanchu.php?sno={$attr[0]}'>删除</a>
 <a href='xiugai.php?sno={$attr[0]}'>修改</a> </td> </tr>"; } } ?>




</table>
<a href="tianjia.php" rel="external nofollow" >添加页面</a>
</body>

</html>
<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>

2.删除的处理页面

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 "添加失败!";
}
<?php
$aaa = $_GET ["sno"]; //删除方式使用的get,照旧
$db = new mysqli("localhost","root","12345678","heiheihei");
//连接...
$sql = "delete from student WHERE sno='{$aaa}'";
//写sql语句,sno主键
if($db->query($sql)) //执行sql语句
{
  header("location:text.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 "删除失败!";
}

图片 1

Update页面(修改数据页面)

3.添加数据:

<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>

点击图片 2即可进入添加页面

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 "修改失败!";
}
<body>

<h1>添加</h1>
<form action="add.php" method="post" >
  <div>代号:<input type="text" name="sno"/></div>
  <div>名字:<input type="text" name="sname"/></div>
  <div>性别: <input type="radio" value="1" name="sex" />男
    <input type="radio" value="0" name="sex"/>女</div>
  <div>日期:<input type="text" name="sbirthday"/></div>
//创建表时性别是用的1或2来表示的,要是进行修改不知道1或2代表了什么,所以就要进行处理,处理成用户能够明白的男和女

  <div>班级:
  <select name="class">

  <?php
    $db= new MYSQLi("localhost","root","12345678","heiheihei");
//连接...
    $sql = " select * from class ";
//写sql...    

    $r = $db->query($sql);
//执行...返回...
    while($arr = $r->fetch_row())
    {
      echo "<option value='{$arr[0]}'>{$arr[1]}</option>";
      //添上以后回表页面
    }
    ?>


  </select>
  </div>
  <div><input type="submit" value="添加"/></div>
</form>

</body>

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 澳门金沙30064在线网站 版权所有