面向对象数据访问添加数据和删除数据

一、增加数据

用bootstrap做

    <link href="bootstrap.min.css" rel="stylesheet" type="text/css" />
    <script src="bootstrap.min.js"></script>
    <script src="jquery-1.11.2.min.js"></script>

 然后访问数据库

<body>
    <table class="table table-striped">
      <caption>人员信息展示</caption>
      <thead>
        <tr>
          <th>代号</th>
          <th>姓名</th>
          <th>性别</th>
          <th>民族</th>
          <th>生日</th>
        </tr>
      </thead>
     <tbody>
     	<?php
			$a = new MySQLi("localhost","root","123456","crud");
			if(mysqli_connect_error()){
				echo "错误";
			}
			$sql = "select info.code,info.name,sex,nation.name,birthday from info,nation where info.nation=nation.code";
			$result = $a->query($sql);
			if($result){
				$attr = $result->fetch_all();
				foreach($attr as $v){
					$sex = $v[2]?"男":"女";//三元运算 把性别的1/0(1为ture 0为false)改为男女
					echo "<tr>
				<td>{$v[0]}</td>
				<td>{$v[1]}</td>
				<td>{$sex}</td>
				<td>{$v[3]}</td>
				<td>{$v[4]}</td>
			 </tr>";
				}
			}else{
			    echo "出错";
			}
        ?>
     </tbody>
    </table>
	<div><a href="tianjia.php">添加数据</a></div> 
</body>

 

tianjia.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>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="bootstrap/js/jquery-1.11.2.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
</head>

<body>
<h1>添加数据</h1>

<form action="./add.php" method="post">
<div class="panel panel-default" style="max-500px">
    <div class="panel-body">
        
         <div class="input-group">
            <span class="input-group-addon">代号:</span>
            <input type="text" class="form-control" placeholder="请输入代号" name="code">
        </div>
        <br />
        <div class="input-group">
            <span class="input-group-addon">姓名:</span>
            <input type="text" class="form-control" placeholder="请输入姓名" name="name">
        </div>
        <br />
        <div class="input-group">
            <span class="input-group-addon">性别:</span>
              
            男<input type="radio" name="sex" value="1" />
             
            女<input type="radio" name="sex" value="0" />
        </div>
        <br />
        <div class="input-group">
            <span class="input-group-addon">民族:</span>
              
           	<select name="nation">
            	<?php
				$db = new MySQLi("localhost","root","123","mydb");
				$sql = "select * from nation";
				$result = $db->query($sql);
				if($result){
					$arr = $result->fetch_all();
					foreach($arr as $v){
						echo "<option value='{$v[0]}'>{$v[1]}</option>";
					}
				}
				?>
            </select>
        </div>
        <br />
        <div class="input-group">
            <span class="input-group-addon">生日:</span>
            <input type="text" class="form-control" placeholder="请输入生日" name="birthday">
        </div>
        <br />
        <input class="btn btn-default" type="submit" value="添加数据">
        
    </div>
</div>
</form>
</body>

add.php文件

<?php
$code = $_POST["code"];
$name = $_POST["name"];
$sex = (int)$_POST["sex"];
$nation = $_POST["nation"];
$birthday = $_POST["birthday"];

$db = new MySQLi("localhost","root","123456","crud");
if(mysqli_connect_error()){
	echo "错误";
}
$sql = "insert into info values('{$code}','{$name}',{$sex},'{$nation}','{$birthday}')";
$result = $db->query($sql);
if($result){
	echo "<script type='text/javascript'>window.location.href='tianjia.php';</script>";
}else{
	echo"添加失败";	
}

二、删除数据

<!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>
    <link href="bootstrap.min.css" rel="stylesheet" type="text/css" />
    <script src="bootstrap.min.js"></script>
    <script src="jquery-1.11.2.min.js"></script>
</head>

<body>
<table class="table table-striped">
      <caption>人员信息展示</caption>
      <thead>
        <tr>
          <th>代号</th>
          <th>姓名</th>
          <th>性别</th>
          <th>民族</th>
          <th>生日</th>
          <th>操作</th>
        </tr>
      </thead>
     <tbody>
     	<?php
			$a = new MySQLi("localhost","root","123456","crud");
			if(mysqli_connect_error()){
				echo "错误";
			}
			$sql = "select info.code,info.name,sex,nation.name,birthday from info,nation where info.nation=nation.code";
			$result = $a->query($sql);
			if($result){
				$attr = $result->fetch_all();
				foreach($attr as $v){
					$sex = $v[2]?"男":"女";//三元运算 把性别的1/0(1为ture 0为false)改为男女
					echo "<tr>
				<td>{$v[0]}</td>
				<td>{$v[1]}</td>
				<td>{$sex}</td>
				<td>{$v[3]}</td>
				<td>{$v[4]}</td>
				<td><a href='delete.php?code={$v[0]}' onclick="return confirm('确认删除么?')"><button type='button' class='btn btn-primary btn-sm'>删除</button></a></td>
			 </tr>";
				}
			}else{
			    echo "出错";
			}
        ?>
     </tbody>
    </table> 
</body>
</html>

delete.PHP文件

<?php
$code = $_GET["code"];
$db = new MySQLi("localhost","root","123","mydb");
$sql = "delete from info where code='{$code}'";
$result = $db->query($sql);
if($result){
	header("location:main.php");
}else{
	echo "删除失败!";
}
原文地址:https://www.cnblogs.com/navyouth/p/8283667.html