:::

5-3 post.php

<?php
  // 啟用 session
  session_start();

  /*----引入檔案----*/
  require_once "config.php";
  require_once "function.php";



  /*----整理傳進來的變數或變數初始值----*/

  //設定樣板中 [var.變數] 對應到的幾個變數其預設值
  $news_title=isset($_SESSION['news_title'])?$_SESSION['news_title']:"";
  $news_content=isset($_SESSION['news_content'])?$_SESSION['news_content']:"";
  $author=isset($_SESSION['author'])?$_SESSION['author']:"";
  $ip=$_SERVER['REMOTE_ADDR'];
  $passwd_error=$next_op='';

  $op=isset($_REQUEST['op'])?$_REQUEST['op']:"";
  $sn=isset($_REQUEST['sn'])?intval($_REQUEST['sn']):"";


  /*----流程控制----*/

  switch ($op) {
    case 'passwd_error':
      $passwd_error=error_msg("密碼錯誤!!請檢查密碼是否正確!");
      break;

    case 'db_error':
      $passwd_error=error_msg("資料庫連線錯誤!!請檢查資料庫帳號、密碼是否正確!");
      break;

    case 'save':
      save_news();
      header("location:index.php");
      exit;
      break;

    case 'update':
      update_news();
      header("location:index.php?op=view&sn={$sn}");
      exit;
      break;

     case 'modify':
       link_db();
       //讀取eznews資料表所有欄位,並指定某一筆特定資料
       $sql="select * from eznews where sn='$sn'";

       //傳回值存到 $result 以供抓取資料用
       $result=mysql_query($sql) or die("{$sql}<br>".mysql_error());
       $news=mysql_fetch_assoc($result);
       $news_title=$news['news_title'];
       $news_content=$news['news_content'];
       $next_op="update";
       break;

    default:
      $next_op="save";
      break;
  }


  /*----輸出----*/
  show_page('post_tpl');



  /*----所有函數----*/
  //顯示錯誤訊息
  function error_msg($msg=""){
    $passwd_error="
    <div class='alert alert-danger'>
      $msg
    </div>";
    return $passwd_error;
  }

  //儲存新聞
  function save_news(){

    //過濾外面傳來的變數
    $op=isset($_POST['op'])?$_POST['op']:"";
    $password=isset($_POST['password'])?$_POST['password']:"";
    $news_title=isset($_POST['news_title'])?$_POST['news_title']:"";
    $news_content=isset($_POST['news_content'])?$_POST['news_content']:"";
    $author=isset($_POST['author'])?$_POST['author']:"";

    //接收使用者輸入密碼,失敗轉回發布頁
    if(empty($password) or $password!=_POST_PASSWD){
      // 產生 cookie
      // setcookie('news_title',$news_title);
      // setcookie('news_content',$news_content);

      // 產生 session,以記住剛剛使用者輸入的資料,避免需要重打
      $_SESSION['news_title']=$news_title;
      $_SESSION['news_content']=$news_content;

      //轉回編輯頁面,並用 get 方式,傳遞 $op 以便顯示密碼有誤的訊息
      header("location:{$_SERVER['PHP_SELF']}?op=passwd_error");
      exit;
    }else{
      // nl2br()將換行符號轉換為<br>標籤
      //$news_content=nl2br($news_content);
      // 發布正確,剛剛輸入的內容無需在記住,因此清空之。
      $_SESSION['news_title']=$_SESSION['news_content']='';
    }

    if(!link_db()){
      $_SESSION['news_title']=$news_title;
      $_SESSION['news_content']=$news_content;

      //轉回編輯頁面,並用 get 方式,傳遞 $op 以便顯示密碼有誤的訊息
      header("location:{$_SERVER['PHP_SELF']}?op=db_error");
      exit;
    }

    //存到資料庫
    $sql="insert into eznews (news_title, news_content, ip, author, post_time) values('$news_title', '$news_content', '$ip', '$author', now())";
    mysql_query($sql) or die($sql."<br>".mysql_error());
  }



  //更新新聞
  function update_news(){

    //過濾外面傳來的變數
    $password=isset($_POST['password'])?$_POST['password']:"";
    $news_title=isset($_POST['news_title'])?$_POST['news_title']:"";
    $news_content=isset($_POST['news_content'])?$_POST['news_content']:"";
    $author=isset($_POST['author'])?$_POST['author']:"";
    $sn=isset($_POST['sn'])?intval($_POST['sn']):"";

    //接收使用者輸入密碼,失敗轉回發布頁
    if(empty($password) or $password!=_POST_PASSWD){
      // 產生 cookie
      // setcookie('news_title',$news_title);
      // setcookie('news_content',$news_content);

      // 產生 session,以記住剛剛使用者輸入的資料,避免需要重打
      $_SESSION['news_title']=$news_title;
      $_SESSION['news_content']=$news_content;

      //轉回編輯頁面,並用 get 方式,傳遞 $op 以便顯示密碼有誤的訊息
      header("location:{$_SERVER['PHP_SELF']}?op=passwd_error");
      exit;
    }else{
      // nl2br()將換行符號轉換為<br>標籤
      //$news_content=nl2br($news_content);
      // 發布正確,剛剛輸入的內容無需在記住,因此清空之。
      $_SESSION['news_title']=$_SESSION['news_content']='';
    }

    if(!link_db()){
      $_SESSION['news_title']=$news_title;
      $_SESSION['news_content']=$news_content;

      //轉回編輯頁面,並用 get 方式,傳遞 $op 以便顯示密碼有誤的訊息
      header("location:{$_SERVER['PHP_SELF']}?op=db_error");
      exit;
    }

    //存到資料庫
    $sql="update eznews set news_title='$news_title', news_content='$news_content', ip='$ip', author='$author', post_time=now() where sn='$sn'";
    mysql_query($sql) or die($sql."<br>".mysql_error());
  }
?>

 


:::

搜尋

QR Code 區塊

https%3A%2F%2Fwww.tad0616.net%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbsn%3D34%26tbdsn%3D1066

書籍目錄

展開 | 闔起

線上使用者

31人線上 (3人在瀏覽線上書籍)

會員: 0

訪客: 31

更多…