namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
class ZsgcController extends Controller
{
//關閉csrf攻擊
/**
* @inheritdoc
*/
//展示添加頁面
{
return $this->render('add');
}
//執行添加
$arr = $_POST;
// var_dump($arr);die;
$db = Yii::$app->db;
$data=$db->createCommand()->insert('zsgc',$arr)->execute();
if($data){
echo "<script>alert('添加成功')</script>";
//添加成功跳轉到我們的展示頁面(show)
$this->redirect('index.php?r=zsgc/show');
}else{
echo "添加失敗";
$this->redirect('index.php?r=zsgc/add');
}
}
//執行展示頁面
{
$db = Yii::$app->db;
$sql="SELECT * FROM zsgc";
$arr = $db->createCommand($sql)->queryAll();
//print_r($arr);die;
return $this->render('show',['data'=>$arr]);
}
//執行刪除
//接id;
$id=$_GET['id'];
// print_r($id);die;
$result=Yii::$app->db->createCommand()->delete('zsgc', "id =$id")->execute();
//print_r($res);die;
if($result){
echo "<script>alert('刪除成功')</script>";
//添加成功跳轉到我們的展示頁面(lists)
$this->redirect('index.php?r=zsgc/show');
}else{
echo "刪除失敗";
$this->redirect('index.php?r=zsgc/show');
}
}
//通過id要修改的頁面
$id=$_GET['id'];
// echo $id;die;
$command = \Yii::$app->db->createCommand("SELECT * FROM zsgc WHERE id=$id");
$post = $command->queryOne();
return $this->render('update',['list'=>$post]);
}
//執行修改
$data=$_POST;
// print_r($data);die;
$res = \Yii::$app->db->createCommand()->update('zsgc', $data,["id" => $data['id']])->execute();
// print_r($res);
if($res){
echo "<script>alert('修改成功')</script>";
//添加成功跳轉到我們的展示頁面(lists)
$this->redirect('index.php?r=zsgc/show');
}else{
echo "修改失敗";
$this->redirect('index.php?r=zsgc/show');
}
}
}