<?php //处理操作页面 //var_dump($_GET); $link = mysqli_connect('localhost','root','123456'); if (mysqli_connect_errno($link)>0) { echo mysqli_connect_error($link);exit; } mysqli_select_db($link,'ss34'); mysqli_set_charset($link,'utf8'); $a = $_GET['a']; switch ($a){ case 'add': //接受数据 $name = $_POST['name']; $sex = $_POST['sex']; $age = $_POST['age']; $city = $_POST['city']; //将我们得到的数据插入到数据库 $sql="INSERT INTO info(id,name,sex,age,city) VALUES(NULL,'{$name}','{$sex}','{$age}','{$city}')"; //echo $sql;exit; $result = mysqli_query($link,$sql); if($result && mysqli_affected_rows($link)>0){ echo '添加成功<a href="index.php">返回</a>'; }else{ echo '添加失败<a href="add.html">返回</a>'; } break; case 'del': $id = $_GET['id']; $sql="DELETE FROM info WHERE id={$id}"; $result = mysqli_query($link,$sql); if($result){ header('location:index.php'); //echo '删除成功'; }else{ header('location:index.php'); //echo '删除失败'; } break; case 'edit': $name = $_POST['name']; $sex = $_POST['sex']; $age = $_POST['age']; $city = $_POST['city']; $id = $_POST['id']; //判断我们修改的数据不能为空 foreach($_POST as $value){ if($value ==''){ echo '重新填写';exit; } } $sql="UPDATE info SET name='{$name}',sex='{$sex}',age='{$age}',city='{$city}' WHERE id={$id}"; //echo $sql; $result = mysqli_query($link,$sql); if($result && mysqli_affected_rows($link)>0){ echo '修改成功<a href="./index.php">返回</a>'; }else{ echo '修改失败<a href="edit.php?id='.$id.'">返回</a>'; } break; default: include './404.html'; break; } // if ($_GET['a'] == 'add') { // //接受数据 // $name = $_POST['name']; // $sex = $_POST['sex']; // $age = $_POST['age']; // $city = $_POST['city']; // //将我们得到的数据插入到数据库 // $sql="INSERT INTO info(id,name,sex,age,city) VALUES(NULL,'{$name}','{$sex}','{$age}','{$city}')"; // //echo $sql;exit; // $result = mysqli_query($link,$sql); // if($result && mysqli_affected_rows($link)>0){ // echo '添加成功<a href="index.php">返回</a>'; // }else{ // echo '添加失败<a href="add.html">返回</a>'; // } // } // if($_GET['a'] =='del'){ // $id = $_GET['id']; // $sql="DELETE FROM info WHERE id={$id}"; // $result = mysqli_query($link,$sql); // if($result){ // header('location:index.php'); // //echo '删除成功'; // }else{ // header('location:index.php'); // //echo '删除失败'; // } // } // if($_GET['a']=='edit'){ // $name = $_POST['name']; // $sex = $_POST['sex']; // $age = $_POST['age']; // $city = $_POST['city']; // $id = $_POST['id']; // //判断我们修改的数据不能为空 // foreach($_POST as $value){ // if($value ==''){ // echo '重新填写';exit; // } // } // $sql="UPDATE info SET name='{$name}',sex='{$sex}',age='{$age}',city='{$city}' WHERE id={$id}"; // //echo $sql; // $result = mysqli_query($link,$sql); // if($result && mysqli_affected_rows($link)>0){ // echo '修改成功<a href="./index.php">返回</a>'; // }else{ // echo '修改失败<a href="edit.php?id='.$id.'">返回</a>'; // } // } mysqli_close($link);
点击 "运行实例" 按钮查看在线实例