接收从add.php页面传过来的数据,将数据保存到数据库中
提示:本教程是将添加,修改,删除,的PHP代码放在同一个PHP页面,我们用了switch,case语句,下面的代码是不完整的action.php 文件代码,等我们将添加,修改,删除的功能完成,会将完整的代码展示给大家
先链接我们的数据库
header("content-type:text/html;charset=utf8");
$conn=mysqli_connect("localhost","root","root","study");
mysqli_set_charset($conn,"utf8");
接收我们从添加页面传过来的信息
$name = $_POST['name'];
$sex = $_POST['sex'];
$age = $_POST['age'];
$class = $_POST['class'];
在对我们的数据库进行数据插入
$sql = "insert into stu (`name`, sex, age, class) values ('$name', '$sex','$age','$class')";
$rw = mysqli_query($conn,$sql);
添加信息代码如下<?php
header("content-type:text/html;charset=utf8");
$conn=mysqli_connect("localhost","root","root","study");
mysqli_set_charset($conn,"utf8");
if($conn){
switch ($_GET['action']){
case 'add'://add
$name = $_POST['name'];
$sex = $_POST['sex'];
$age = $_POST['age'];
$class = $_POST['class'];
$sql = "insert into stu (`name`, sex, age, class) values ('$name', '$sex','$age','$class')";
$rw = mysqli_query($conn,$sql);
if ($rw > 0){
echo "";
}else{
echo "";
}
}
}
header('Location: index.php');
break;
?>
下一步就是将我们添加的信息展示出来