前提
気付けばプロ並みphpという書籍で学習しながら、自分でstaff_listにデータベースのスタッフ名とその下に追加、修正、削除のボタンを表示し、押したボタンに応じてstaff_branch.phpをとおってそれぞれのページに飛べるようなものを作っています。
実現したいこと
staff_listにデータベースのスタッフ名とその下に追加、修正、削除のボタンを表示し、押したボタンに応じてstaff_branch.phpをとおってそれぞれのページに飛べるようにしたいです。
発生している問題・エラーメッセージ
http://localhost/shopra/staff_branch.php?staffcode=1&delete=%E5%89%8A%E9%99%A4のように削除したい場合はstaff_delete_phpに移動するはずなのですが、移動できず、画面は真っ白になっていて何も表示されません。
該当のソースコード
staff_list.php <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <?php try { $dbn = 'mysql:dbname=shop;lost=localhost;charset=utf8'; $user = 'root'; $password = ''; $dbh = new PDO($dbn, $user, $password); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = 'select code, name from mst_staff where 1'; $stmt = $dbh->prepare($sql); if (!$stmt) { die($dbh->error); } $success = $stmt->execute(); if (!$success) { die($dbh->error); } $dbn = null; echo 'スタッフ一覧 <br><br>'; echo '<form action ="staff_branch.php" action="post">'; while (true) { $rec = $stmt->fetch(PDO::FETCH_ASSOC); if ($rec == false) { break; } echo '<input type="radio" name="staffcode" value="'.$rec['code'].'">'; echo $rec['name']; echo $rec['code']; echo '<br>'; } echo '<input type="submit" value="追加" name="add">'; echo '<input type="submit" value="修正" name="edit">'; echo '<input type="submit" value="削除" name="delete">'; echo '</form>'; } catch (Exception $e) { $e->getMessage(); } ?> </body> </html> staff_branch.php <?php $staff_code = filter_input(INPUT_POST, 'staffcode', FILTER_SANITIZE_SPECIAL_CHARS); $staff_delete = filter_input(INPUT_POST, 'delete', FILTER_SANITIZE_SPECIAL_CHARS); $staff_add = filter_input(INPUT_POST, 'add', FILTER_SANITIZE_SPECIAL_CHARS); $staff_edit = filter_input(INPUT_POST, 'edit', FILTER_SANITIZE_SPECIAL_CHARS); if(isset($staff_add)==true) { header('Location:staff_add.php'); exit(); } if (isset($staff_edit)==true) { if(isset($staff_code) == false) { header('Location:staff_ng.php'); exit(); } header('Location:staff_edit.php?staffcode='.$staff_code); exit(); } if (isset($staff_delete)==true) { if(isset($staff_code) == false) { header('Location:staff_ng.php'); exit(); } header('Location:staff_delete.php?staffcode='.$staff_code); exit(); } ?>
試したこと
教科書上では$staff_code = $_POST{staff_code}というように記されているところをfilter_inputを用いてコードを書いています。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/11/20 11:00