<?php
require_once "pdo.php";
session_start();

    if ( isset($_POST['article']) && isset($_SESSION['user_id']) && isset($_POST['title'])&&isset($_POST['article_type'])) {
        if ( isset($_POST['article_id']) &&$_POST['article_id']!=null){
            $sql = "UPDATE article SET content=:articles,headline=:headline,category=:category,public=:public  WHERE article_id = :id ";
             $stmt = $pdo->prepare($sql);
             try{
                $stmt->execute(array(
                    ':articles' => $_POST['article'],
                    ':headline' => $_POST['title'],
                    ':public' => $_POST['article_type'],
                    ':id'=>$_POST['article_id'],
                    ':category'=>$_POST['category']
                ));
                }catch (Exception $e) {
                    $_SESSION['error'] = 'fail';
                    header( 'Location: edit_article.php' ) ;
                    return;
                }
                $id=$_POST['article_id'];
        }
        else{
        $sql = "INSERT INTO article (content,user_id,headline,public,category,viewer) VALUES (:articles, :user_id,:headline,:public,:category,:viwer)";
        $stmt = $pdo->prepare($sql);
       
    try{
        $stmt->execute(array(
            ':articles' => $_POST['article'],
            ':user_id' => $_SESSION['user_id'],
            ':headline' => $_POST['title'],
            'viwer'=>0,
            ':public' => $_POST['article_type'],
            ':category'=>$_POST['category']
        ));
        }catch (Exception $e) {
            $_SESSION['error'] = 'fail';
            header( 'Location: edit_article.php' ) ;
            return;
        }
       
        $id=$pdo->lastInsertId();
    }
    $_SESSION['success'] = '保存成功';
header( 'Location:view.php?article_id='.$id ) ;
return;
}