/* Google Analytics ▽ */ /* Google Analytics △ */

WEB制作の勉強メモ / 猫脳人間

HTML CSS JavaScript ActionScript jQuery WordPress PHP Illustrator Photoshop Flash … 猫サイズの脳でも覚えるぞ と…

PHPの授業:記事の修正等

PHPの記事を修正する授業の一環
※授業中の記述です。
 完成ではありません。

PHP: end - Manual

$_SESSION['DELETE'] = array();
$_SESSION['INSERT'] = array();
$_SESSION['UPDATE'] = array();

unset($_SESSION['DELETE']);
unset($_SESSION['INSERT']);
unset($_SESSION['UPDATE']);

上記の記述を下記の記述に書き直し

$clear_array =array('DELETE','INSERT','UPDATE');
foreach($clear_array as $v){
  if(isset($_SESSION[$v])){
    $_SESSION[$v] = array();
    unset($_SESSION[$v]);
  }
}
if(isset($_SESSION['UPDATE']['news_headline']) && isset($_SESSION['UPDATE']['news_date']) && isset($_SESSION['UPDATE']['news_article']) && isset($_SESSION['UPDATE']['news_img'])){
  $news_headline = $_SESSION['UPDATE']['news_headline'];
  $news_date = $_SESSION['UPDATE']['news_date'];
  $news_article = $_SESSION['UPDATE']['news_article'];
  $news_img = $_SESSION['UPDATE']['news_img'];
}
上記の記述を下記の記述に書き直し
if(isset($_SESSION['UPDATE']) && $news_id === $_SESSION['UPDATE']['news_id']){
  foreach($form_data as $v){
    if(isset($_SESSION['UPDATE'][$v])){
      $$v = $_SESSION['UPDATE'][$v];
    }
  }
}

update_entry.php

<?php

/*
if(!isset($_GET['news_id']) || !is_numeric($_GET['news_id']) ||$_GET['news_id'] <= 0){
  header('Location:news_list.php');
  exit;
}
*/

require_once dirname(__FILE__).'/../func.php';

$form_data =array('news_headline','news_date','news_article','news_img');

$news_id = $_GET['news_id'];
$dbh = new PDO(DB_HOST,DB_USER,DB_PASS);
$sql = 'SELECT news_article,news_id,news_headline, news_date,news_img FROM news WHERE news_id=:news_id;';
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':news_id',$news_id);
$stmt->execute();
$rec = $stmt->fetch(PDO::FETCH_ASSOC);
if(!$rec){
  header('Location:news_list.php');
  exit;
}
foreach($form_data as $v){
  $$v = $rec[$v];
}

session_start();

foreach($form_data as $v){
  $v.='_error';
  if(isset($_SESSION['UPDATE'][$v])){
    $$v = $_SESSION['UPDATE'][$v];
  } else {
    $$v = '';
  }
}

if(isset($_SESSION['UPDATE']) && $news_id === $_SESSION['UPDATE']['news_id']){
  foreach($form_data as $v){
    if(isset($_SESSION['UPDATE'][$v])){
      $$v = $_SESSION['UPDATE'][$v];
    }
  }
}

/* 下記の記述を上記にして記述を短く
  if(isset($_SESSION['UPDATE']['news_headline']) && isset($_SESSION['UPDATE']['news_date']) && isset($_SESSION['UPDATE']['news_article']) && isset($_SESSION['UPDATE']['news_img'])){
    $news_headline = $_SESSION['UPDATE']['news_headline'];
    $news_date = $_SESSION['UPDATE']['news_date'];
    $news_article = $_SESSION['UPDATE']['news_article'];
    $news_img = $_SESSION['UPDATE']['news_img'];
  }
*/



$_SESSION['UPDATE']['news_id']=$_GET['news_id'];

?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>ニュース修正入力</title>
</head>
<body>
<h1>ニュース修正入力</h1>
<form action="update_confirm.php" method="post">
<table>
<tr>
<th><label for="news_headline">見出し</label>50文字以内</th><td><input type="text" id="news_headline" name="news_headline" value="<?php echo h($news_headline);?>"><br><span><?php if(!empty($news_headline_error)) echo h($news_headline_error); ?></span>
</td>
</tr>
<tr>
<th><label for="news_date">日付</label><br>日付は2015-07-24 18:40:20の形式で入力してください</th><td><input type="text" id="news_date" name="news_date" value="<?php echo h($news_date)?>"><br><span><?php if(!empty($news_date_error)) echo h($news_date_error);?></span></td>
</tr>
<tr>
<th><label for="news_img">画像ファイル名</label>128文字以内</th><td><input type="text" id="news_img" name="news_img" value="<?php echo h($news_img)?>"><br><span><?php if(!empty($news_img_error)) echo h($news_img_error);?></span></td>
</tr>
<tr>
<th><label for="news_article">記事</label>21000文字以内</th><td><textarea id="news_article" name="news_article"><?php echo h($news_article);?></textarea><br><span><?php  if(!empty($news_article_error)) echo h($news_article_error);?></span></td>
</tr>
</table>
<p><input type="submit" value="確認"></p>
</form>
<p><a href="news_list.php">ニュース一覧(編集)</a></p>
</body>
</html>

update_confirm.php

<?php
if(!isset($_POST['news_headline'])||!isset($_POST['news_date'])||!isset($_POST['news_article'])||!isset($_POST['news_img'])){
  header('Location:news_list.php');
  exit;
}
require_once dirname(__FILE__).'/../func.php';
$form_data =array('news_headline','news_date','news_article','news_img');

foreach($form_data as $v){
  $$v = $_POST[$v];
}
$success= true;
foreach($form_data as $v){
  $v .='_error';
  $$v ='';
}
$path = dirname(__FILE__).'/img/'.$news_img;
if(!empty($news_img) && !file_exists($path)){
  $news_img_error ='画像がアップロードされていません。';
  $success = false;
}

$date_check=date_parse_from_format('Y-m-d H:i:s',$news_date);
if( $date_check['error_count']>0 ||$date_check['warning_count']>0 ){
  $news_date_error = '不正な日付方式です。例:2014-10-23 17:25:21の形式で入力してください';
  $success = false;
}

if(mb_strlen($news_headline,'utf-8')>50){
  $news_headline_error ='見出しが長すぎます。';
  $success = false;
}
if(mb_strlen($news_article,'utf-8')>21000){
  $news_article_error ='記事が長すぎます。';
  $success = false;
}

if(empty($news_headline)){
  $news_headline_error ='見出しが入力されてません。';
  $success = false;
}
if(empty($news_article)){
  $news_article_error ='記事が入力されてません。';
  $success = false;
}

session_start();
foreach($form_data as $v){
  $_SESSION['UPDATE'][$v] = $_POST[$v];
}
//$_SESSION['INSERT']['news_article_error']=$news_article_error;
foreach($form_data as $v){
  $v .='_error';
  $_SESSION['UPDATE'][$v] = $$v;
}
$_SESSION['UPDATE']['success'] = $success;

$news_id = $_SESSION['UPDATE']['news_id'];
if(!$success){
 // header('Location:update_entry.php?news_id='. $news_id. "'");
  header("Location:update_entry.php?news_id={$news_id}");
  exit;
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>ニュース修正確認</title>
</head>
<body>
<h1>ニュース修正確認</h1>
<dl>
<?php
echo '<dt>';
echo h($news_headline);
echo '<span>';
echo h($news_date);
echo '</span>';
echo '</dt>';
echo '<dd>';
echo nl2br(h($news_article));
//echo h($news_img);
if(!empty($news_img)){
  echo '<br><img src="img/' .h($news_img).'" >';
}
echo '</dd>';

?>
</dl>
<p><a href="news_list.php">ニュース一覧(編集)</a>
<a href="update_entry.php?news_id=<?php echo h($news_id); ?>">戻る</a>
<a href="update_fin.php">修正実行</a>
</p>

</body>
</html>

update_fin.php

<?php
require_once dirname(__FILE__).'/../func.php';
$form_data =array('news_headline','news_date','news_article','news_img','news_id');
session_start();
if(!$_SESSION['UPDATE']['success']){
  header('Location:news_list.php');
  exit;
}

foreach($form_data as $v){
  $$v = $_SESSION['UPDATE'][$v];
}
$_SESSION['UPDATE'] =array();
unset($_SESSION['UPDATE']);
$dbh = new PDO(DB_HOST,DB_USER,DB_PASS);

$sql = 'UPDATE news SET news_headline=:news_headline,news_date=:news_date,news_article = :news_article,news_img = :news_img WHERE news_id = :news_id;';


$stmt = $dbh->prepare($sql);
foreach($form_data as $v){
  $stmt->bindParam(":{$v}",$$v);
}
$stmt->execute();
//header('Location:news_list.php');
思考訓練遊び

www.sudokugame.org