###前提・実現したいこと
PHPで従業員管理画面をつくっています。
従業員のデータ削除機能がGETメゾットで作られているので、
これをPOSTメゾットを使ってコードを書き直したいと考えております。
###発生している問題
DELETEをPOSTメゾットで使う方法を検索したのでが、理解が追いつかずこちらに質問させていただいております。
###該当のソースコード
<?php session_start(); ?> <html> <head> <title>PHP学習用</title> </head> <body> <h1>スタッフ名簿一覧</h1> <?php require_once("MYDB.php"); $pdo = db_connect(); if(isset($_GET['action']) && $_GET['action'] == 'delete' && $_GET['id']>0){ try{ $pdo->beginTransaction(); $id = $_GET['id']; $sql = "DELETE FROM staffs WHERE id =:id"; $stmh = $pdo->prepare($sql); $stmh->bindValue(':id',$id, PDO::PARAM_INT); $stmh->execute(); $pdo->commit(); print"データを".$stmh->rowCount()."件,削除しました。"; echo "<br>"; echo "削除後のスタッフ一覧になります。"; }catch(PDOException $Exception){ $pdo->rollBack(); print"エラー:".$Exception->getMessage(); } } try{ $sql = "SELECT * FROM staffs"; $stmh = $pdo->prepare($sql); $stmh->execute(); }catch(PDOException $Exception){ die('接続エラー:' .$Exception->getMessage()); } ?> <table width = "450" border='1' cellspacing="0" cellpadding="8"> <tbody> <tr><th>ID</th><th>名前</th><th>生年月日</th><th> </th><th> </th></tr> <?php while($row = $stmh->fetch(PDO::FETCH_ASSOC)){ ?> <tr> <th><?=htmlspecialchars($row['id'])?></th> <th><?=htmlspecialchars($row['name'])?></th> <th><?=htmlspecialchars($row['birthday'])?></th> <th align="center"><a href = list.php?action=delete&id=<?=htmlspecialchars($row['id'])?>>削除</a></th> </tr> <?php }; $pdo = null; ?> </tbody></table> </body> </html>
###試したこと
下記のコードを書き換えることでPOSTメゾットで削除機能ができると考えました。
if(isset($_GET['action']) && $_GET['action'] == 'delete' && $_GET['id']>0
<th align="center"><a href = list.php?action=delete&id=<?=htmlspecialchars($row['id'])?>>削除</a></th>
テキストリンクではなく、formタグを使ってPOSTの削除ボタンを作ろうとしましたが、
そのときのコードの書き換えがわかりませんでした。
他にjavascriptを使う方法も検索ででてきましたが、今回は使わずに解決できればと思います。
どうぞよろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/17 06:13
2017/06/18 06:40
2017/06/19 02:10