fork download
  1. <?php
  2.  
  3. try {
  4.  
  5. // パラメータを$idに代入
  6. $num = '08241';
  7. if (!isset($_POST[$num]) || !is_string($id = $_POST[$num])) {
  8. throw new Exception('パラメータが不正です');
  9. }
  10.  
  11. // PDOオブジェクト生成
  12. $pdo = new PDO(
  13. 'mysql:dbname=testdb',
  14. 'user',
  15. 'password',
  16. PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
  17. )
  18. );
  19.  
  20. // SQLエラー時に例外をスローするように設定
  21. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  22.  
  23. // ステートメントを生成して実行し、結果をフェッチする
  24. $stmt = $pdo->prepare('SELECT * FROM testTable WHERE id = ?');
  25. $stmt->execute(array($id));
  26. $raw_data = $stmt->fetch(PDO::FETCH_ASSOC);
  27. if ($raw_data === false) {
  28. throw new Exception('データが見つかりませんでした');
  29. }
  30.  
  31. // HTMLとして表示できるようにエンティティをエスケープ
  32. $data = array_map(function ($input) {
  33. return htmlspecialchars($input, ENT_QUOTES, 'UTF-8');
  34. }, $raw_data);
  35.  
  36. } catch (Exception $e) {
  37.  
  38. // エラーメッセージを$errorに代入
  39. $error = $e->getMessage();
  40.  
  41. }
  42.  
  43.  
  44. ?>
  45. <!DOCTYPE html>
  46. <html>
  47. <head>
  48. <meta charset="UTF-8" />
  49. <title>test</title>
  50. </head>
  51. <body>
  52. <?php if (isset($error)): ?>
  53. <p><?=$error?></p>
  54. <?php else: ?>
  55. <table width="250" border="1">
  56. <tr>
  57. <td width="250">選択したもの</td>
  58. </tr>
  59. <tr>
  60. <td><?=$data['id']?></td>
  61. </tr>
  62. <tr>
  63. <td>
  64. <form name="a11" action="/products/detail.php" method="post" >
  65. <input type="hidden" name="mode" value="cart" />
  66. <input type="hidden" name="product_id" value="<?=$data['product_id']?>" />
  67. <input type="hidden" name="product_class_id" value="<?=$data['product_class_id']?>" />
  68. <input type="hidden" name="quantity" value="1" /><br />
  69. <input type="submit" value="決定する" onClick="alert('選択しました')" />
  70. </form>
  71. </td>
  72. </tr>
  73. </table>
  74. <?php endif; ?>
  75. </body>
  76. </html>
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>test</title>
</head>
<body>
<p>パラメータが不正です</p>
</body>
</html>