action.php
#1.连接数据库
try{
$pdo = new PDO("mysql:host=localhost;dbname=php","root","root");
}catch(PDOException $e){
die("数据库连接失败".$e->getMessage());
}
#2.通过aaction的值判断相应的操作
switch($_GET['action'])
{
case 'add'://添加的操作
$name = $_POST['name'];
$age = $_POST['age'];
$sex = $_POST['sex'];
$sql = "insert into test value(null,'{$name}','{$age}','{$sex}')";
$rw = $pdo->exec($sql);
if($rw > 0)
{
echo "";
}
else
{
echo "";
}
break;
case 'del':
$id = $_GET['id'];
$sql = "delete from test where id = {$id}";
$pdo->exec($sql);
header("location:index.php");
break;
case 'edit':
$id = $_POST['id'];
$name = $_POST['name'];
$age = $_POST['age'];
$sex = $_POST['sex'];
$sql = "update test set name = '{$name}',age = {$age},sex = '{$sex}' where id = {$id}";
$res = $pdo->exec($sql);
#echo $res;
if($res > 0)
{
echo "";
}
else
{
echo "";
}
break;
}?>
|