###前提・実現したいこと
PHPで削除機能を実装しています。
①→②→③のようにしていきたいのですが
③の削除完了の所で「HTTP ERROR 500」とでてしまいます。
###発生している問題・エラーメッセージ
HTTP ERROR 500 と 192.168.33.1:60007 [500]: /delete3.php - syntax error, unexpected '"<a href='delete1.php'>delete1' (T_CONSTANT_ENCAPSED_STRING) in /home/vagrant/public_php/delete3.php on line 12
###該当のソースコード
①一覧画面 delete1.php
php
1<?php 2ini_set("display_errors", On); // エラーを出力する 3error_reporting(E_ALL); //全ての PHP エラーを表示する 4 5//文字コードを指定する 6header("content-type: text/html; charset=utf-8"); 7//外部のファイルの読み込み 8require_once("index_db.php"); 9 10$sql = "select id, rank, nation, food, reason from country"; 11//SQLステートメントを実行し、結果セットを PDOStatement オブジェクトとして返す 12$stmt = $pdo->query($sql); 13 14//結果の取得 15$country = array(); 16 foreach ($stmt as $row) { 17 $country[] = $row; 18} 19 //結果の出力 20 var_dump($country); 21 22//データベース切断 23$pdo = null; 24 25 ?> 26 27 28<!DOCTYPE html> 29<html> 30 <head> 31 <title>国一覧</title> 32 </head> 33 34 <body> 35 36 <h1>国一覧</h1> 37 38 <table border="1"> 39 40 <tr> 41 <td>順位</td> 42 <td>国名</td> 43 <td>食べたい食べ物</td> 44 <td>理由</td> 45 </tr> 46 47 <?php 48 foreach($country as $row) { 49 ?> 50 51 52 <tr> 53 <td><?=htmlspecialchars($row['rank'], ENT_QUOTES, 'UTF-8')?></td> 54 <td><?=htmlspecialchars($row['nation'], ENT_QUOTES, 'UTF-8')?></td> 55 <td><?=htmlspecialchars($row['food'], ENT_QUOTES, 'UTF-8')?></td> 56 <td><?=htmlspecialchars($row['reason'], ENT_QUOTES, 'UTF-8')?></td> 57 <td> 58 <form action="delete2.php" method="post"> 59 <input type="submit" value="削除"> 60 <input type="hidden" name="id" value="<?=$row['id']?>"> 61 <input type="hidden" name="rank" value="<?=$row['rank']?>"> 62 <input type="hidden" name="nation" value="<?=$row['nation']?>"> 63 <input type="hidden" name="food" value="<?=$row['food']?>"> 64 <input type="hidden" name="reason" value="<?=$row['reason']?>"> 65 </form> 66 67 </td> 68 </tr> 69 70 <?php 71 } 72 ?> 73 74 </table> 75 </body> 76</html> 77
②確認画面 delete2.php
php
1<?php 2ini_set("display_errors", On); //エラーを出力する 3error_reporting(E_ALL); //全てのPHPエラーを表示する 4 5//文字コードを指定する 6header("content-type: text/html; charset=utf-8"); 7//外部ファイルの読み込み 8require_once("index_db.php"); 9 10 11//$_POSTで受け取る 12$id = $_POST['id']; 13$rank = $_POST['rank']; 14$nation = $_POST['nation']; 15$food = $_POST['food']; 16$reason = $_POST['reason']; 17 18 19 20?> 21 22 23 24<!DOCTYPE html> 25<html> 26 27 <head> 28 <title>確認画面</title> 29 </head> 30 31 <body> 32 <form action="delete3.php" method="post"> 33 <h1>本当に削除しますか?</h1> 34 <input type="hidden" name="id" value="<?=$id?>"> 35 <input type="hidden" name="rank" value="<?=$rank?>"> 36 <input type="hidden" name="nation" value="<?=$nation?>"> 37 <input type="hidden" name="food" value="<?=$food?>"> 38 <input type="hidden" name="reason" value="<?=$reason?>"> 39 40 <td><?=htmlspecialchars($id, ENT_QUOTES, 'UTF-8')?></td> 41 <td><?=htmlspecialchars($rank, ENT_QUOTES, 'UTF-8')?></td> 42 <td><?=htmlspecialchars($nation, ENT_QUOTES, 'UTF-8')?></td> 43 <td><?=htmlspecialchars($food, ENT_QUOTES, 'UTF-8')?></td> 44 <td><?=htmlspecialchars($reason, ENT_QUOTES, 'UTF-8')?></td> 45 <a href="delete1.php"><input type="button" name="back" value="戻る"></a> 46 <a href="delete3.php"><input type="submit" name="delete" value="削除"></a> 47 </form> 48 </body> 49 50 51</html> 52
③実行画面 delete3.php
php
1<?php 2ini_set("display_errors", On); //エラーを出力する 3error_reporting(E_ALL); //全てのPHPエラーを表示する 4 5//文字コードを指定する 6header("content-type: text/html; charset=utf-8"); 7//外部ファイルの読み込み 8require_once("index_db.php"); 9 10try { 11 12 if (!isset($_POST['id']) || !is_numeric($_POST['id']) ){ 13 echo "IDエラー"; 14 exit(); 15 } else { 16 17 //プリペアドステートメント 18 $stmt = $pdo->prepare("delete from country where id=?"); 19 20 if ($stmt) { 21 //プレースホルダに実際の値を設定する 22 $id = $_POST['id']; 23 $stmt->bindParam(1, $id, PDO::PARAM_INT); 24 //実行する 25 $stmt->execute(); 26 27 if ( $stmt->execute() ) { 28 echo "削除成功"; 29 } else { 30 echo "削除失敗"; 31 } 32 } 33 } 34 35 36 $pdo = null; 37 } 38 } 39 } catch (Exception $e) { 40 echo 'error'; 41 echo('エラーが発生しました。:'.$e->getMessage()); 42 exit; 43} 44 45 46?> 47 48
ご教授いただけたら嬉しいです。
どうか宜しくお願い致します。
###補足情報(言語/FW/ツール等のバージョンなど)
php5.4.6, mysql 14.14
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/09/03 09:55
2017/09/03 10:04
退会済みユーザー
2017/09/03 10:06
2017/09/03 10:09
退会済みユーザー
2017/09/03 10:20
退会済みユーザー
2017/09/03 10:20
2017/09/03 10:21
退会済みユーザー
2017/09/03 10:24 編集
2017/09/03 10:30
2017/09/03 10:30
退会済みユーザー
2017/09/03 10:38
2017/09/03 10:38
退会済みユーザー
2017/09/03 10:39