数据访问之增加、删除


<!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> <h1>主页面</h1> //把Info表查出来,用一个表格显示 <table width="100%" cellpadding="0" cellspacing="0" border="1"> <tr> <td>代号</td> <td>姓名</td> <td>性别</td> <td>民族</td> <td>生日</td> <td>操作</td> </tr> <?php $db=new MySQLi("localhost","root","","mydb"); !mysqli_connect_error() or die ("连接失败"); $sql="select * from Info"; $result=$db->query($sql); $arr=$result->fetch_all(); foreach($arr as $v) { $sex = $v[2]?'男':'女'; //处理民族名称 $sqln = "select Name from Nation where Code='{$v[3]}'"; $r = $db->query($sqln); $attr = $r->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></td> </tr>"; } ?> </table> <div><a href="Add.php">添加数据</a></div> </body> </html>

<body>
<h1>添加数据</h1>
<form action="AddChuLi.php" method="post">
<div>代号:<input type="text" name="code"/></div><br />
<div>姓名:<input type="text" name="name" /></div><br />
<div>性别:
        <input type="radio" value="男" name="sex"/><input type="radio" value="女" name="sex"/></div><br />
<div>民族:
          <select name="nation">
          <?php
          $db=new MySQLi("localhost","root","","mydb");
          $sql="select * from Nation";
          $result=$db->query($sql);
          $attr=$result->fetch_all();
          foreach($attr as $v )
          {
              echo "<option value='{$v[0]}'>{$v[1]}</option>";
          }         
          ?>
          </select></div><br />

<div>生日:<input type="text" name="birthday" /></div><br />
<div><input type="submit" value="添加数据" /></div>
<div><a href="main.php">主页</a></div>


</form>
</body>

<?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");
$sql = "insert into Info values('{$code}','{$name}',{$s},'{$nation}','{$birthday}')";
//var_dump($sql);
$result = $db->query($sql);

if($result)
{
    header("location:Add.php");//跳转到Add.php
}
else
{
    echo "添加失败!";
}
<?php
$code=$_GET["code"];
$db=new MySQLi("localhost","root","","mydb");
!mysqli_connect_error() or die ("连接失败");
$sql="delete from Info where code='{$code}'";
$result=$db->query($sql);
if($result)
{
    echo header("location:main.php");
}
else
{
    echo"删除失败";
}
原文地址:https://www.cnblogs.com/nannan-0305/p/5483491.html