PHPの削除機能が実装できたため、備忘録として説明する。
<form method="get" action="delete.php">
<input type="hidden" name="id" value="<?php echo h($todo["id"]) ?>">
<button >削除</button>
</form>
※前提として$["id"]にidが入っている前提。
↑
データベースで「id」を入れること、
(例: $stmt = $pdo->query("SELECT name, brith, department_id,
account, password, id FROM users"); )
※
がgetであることを確認する。
※「 form 」タグを作り「 delete.php 」でPOST送信する。
//①DBベース接続
$pdo = new PDO("mysql:charset={$char_set};dbname={$db};host={$host}", $user, $password);
// ②新規作成 start このページにアクセスしたら、
// ゲットデータを取得する。
if(isset($_GET["id"])){ //変数に値が入っているかどうかを確認するためのisset関数
$id = $_GET["id"]; //main.phpで入れた内容が入ってくる。
//→POSTに変更するとうまくいく場合がある。
try {
$pdo->beginTransaction();
// ③データを登録するSQL
$stmt = $pdo->prepare("DELETE FROM todo_list WHERE id=:id");
// ④値をセット
$stmt->bindParam( ':id',$id,PDO::PARAM_STR);
// ⑤SQL実行
$res = $stmt->execute();
// コミット
if( $res ) {
$pdo->commit();
}
} catch(PDOException $e) {
// エラーメッセージを出力
echo $e->getMessage();
// ロールバック
$pdo->rollBack();
}
};
//⑤.データ登録処理後
if($res==false){
sql_error($stmt);
}else{
redirect("main.php");
}
?>
①DBベース接続する
②ゲットデータを取得する
③SQLにデータ登録する
③値をセットをする
④SQLの実行をする
⑤データ登録後の処理をする
※IDの動きを意識しながら、作業を行うとうまくいく。