LoginSignup
0
0

More than 1 year has passed since last update.

編集方法

Posted at

PHPで編集を行ったために、編集方法を説明する。

※idを保持している前提

//wait_ad_list.php
ここで編集画面へ飛ぶ

session_start(); //セッションスタート

//画面表示start。
  try {
    // データを取得するSQL文(ここでIDを取得する)
    $stmt = $pdo->query("SELECT created_at, serial_num, discription, ship_quantity, agreement_at, id FROM inspections");

    $reg_list = $stmt->fetchAll(PDO::FETCH_ASSOC);

<button>
    <a href="wait_ad_edit.php?id=<?php echo $regs_list['id'] ?>">編集</a>
</button>

//wait_ad_edit.php
 if(isset($_GET["id"])){  //変数に値が入っているかどうかを確認するためのisset関数
    $id = $_GET["id"];  //$idを受け止める

      try {

        $pdo->beginTransaction();
    
        // データを登録するSQL
        $stmt = $pdo->prepare("SELECT * FROM inspections WHERE id=:id");

        $stmt->bindParam(':id'   , $id,    PDO::PARAM_INT);

        // SQL実行
        $res = $stmt->execute();

        $edits_list = $stmt->fetch(PDO::FETCH_ASSOC); 
    
        // コミット
        if($res) {
          $pdo->commit();
        }
        //ここでトランザクション終了。
    
      } catch(PDOException $e) {
    
        // エラーメッセージを出力
        echo $e->getMessage();
        
        // ロールバック
        $pdo->rollBack();
      }
  }

?>

<form method="post" action="wait_ad_update.php">
        <div class="row">
          <div class="col">検査日:
            <input type="text"  name="created_at" value="<?php echo $edits_list["created_at"]?>">
          </div>
          <div class="col">品番:
            <input type="text"  name="serial_num" value="<?php echo $edits_list["serial_num"]?>">
          </div>
          <div class="col">ロットNo.:
            <input type="text" name="discription" value="<?php echo $edits_list["discription"]?>">
          </div>
          <div class="col">数量:
            <input type="text" name="ship_quantity" value="<?php echo $edits_list["ship_quantity"]?>">
          </div>
          <div class="col">判定:
            <input type="text" name="agreement_at" value="<?php echo $edits_list["agreement_at"]?>">
          </div>
          <div class="col">機能:
//編集ボタン start
            <input type="hidden" name="id" value="<?php echo $edits_list["id"]?>">
            <input type="submit" value="完了"> 
            <input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']?>">
          </div>
//編集ボタン end
        </div> 
      </form>

//formで編集する



<?php
//編集処理画面
//wait_ad_update.php

//最初にSESSIONを開始!!ココ大事!!
session_start();
require_once('common/function.php'); 

if(!isset($_SESSION['id'])){
  header("Location:./login.php");
  exit(); 
}

//データベース接続したものを$pdoに入れる(function.phpにあり) 
$pdo = db();

// ポストデータを取得する。
if(isset($_POST["id"]) || isset($_POST["serial_num"]) || isset($_POST["discription"]) || isset($_POST["ship_quantity"]) || isset($_POST["agreement_at"])){
$id               = $_POST["id"];
$created_at       = $_POST["created_at"];
$serial_num       = $_POST["serial_num"];
$discription      = $_POST["discription"]; 
$ship_quantity    = $_POST["ship_quantity"];
$agreement_at     = $_POST["agreement_at"]; 

  try {
    // トランザクション開始(ロック)...何かの処理をひとまとめにした状態で、何か処理をする中で処理が失敗した場合に最初からやり直すという意味
    $pdo->beginTransaction();

    // データを登録するSQL
    $stmt = $pdo->prepare("UPDATE inspections SET created_at=:created_at, serial_num=:serial_num,
            discription=:discription, ship_quantity=:ship_quantity, agreement_at=:agreement_at where id=:id");

    // 値をセット
    $stmt->bindParam( ':id',            $id,            PDO::PARAM_INT);
    $stmt->bindParam( ':created_at',    $created_at,    PDO::PARAM_STR);
    $stmt->bindParam( ':serial_num',    $serial_num,    PDO::PARAM_STR);
    $stmt->bindParam( ':discription',   $discription,   PDO::PARAM_STR);
    $stmt->bindParam( ':ship_quantity', $ship_quantity, PDO::PARAM_INT);
    $stmt->bindParam( ':agreement_at',  $agreement_at,  PDO::PARAM_STR);


    // SQL実行
    $res = $stmt->execute();

    // コミット
    if( $res ) {
      $pdo->commit();
    }
    //ここでトランザクション終了。
  } catch(PDOException $e) {

    // エラーメッセージを出力
    echo $e->getMessage();
    
    // ロールバック
    $pdo->rollBack();
  }
};


//4.データ登録処理後
if($res==false){
    sql_error($stmt);
  } else {
    redirect("wait_ad_list.php");
}


?>

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0