###前提・実現したいこと
新規登録フォームを作成し、そこからmysqlに繋げて登録しようとしています。
エラー内容が出ずに、データーベースを見てみると登録されていないので困っています。
もしかしたら、文法の書き方に問題があるかと思われますが、自分では解決できないので
ご質問させていただきました。
なぜ、エラーが出ないのかわかりません。
index_db.phpはデーターベースに繋げる専用のファイルで、
それをregistration.phpというファイルにrewuire_onceで受け取っています。
registration.phpはinsertで登録しようとしているファイルです。
###発生している問題・エラーメッセージ
なし
###該当のソースコード
新規登録フォーム
HTML
1<!DOCTYPE html> 2<html lang=“ja”> 3 <head> 4 <meta charset="UTF-8"> 5 <title>My Web Site</title> 6 <link rel="stylesheet" href="style2.css"> 7 8 </head> 9 <body> 10 11 12 13 <header> 14 <div class="header-left"> 15 <img class="logo" src="https://www.fastpic.jp/images.php?file=3519571152.jpg" alt="ロゴ画像"> 16 </div> 17 18</header> 19 20 21 22<div class="main"> 23<ol class="topic-path"> 24 <li><a href="#">一覧へ</a> > </li> 25 <li>登録</li> 26</ol> 27 28 29<div class="container"> 30<h1>登録</h1> 31 <hr> 32 33 34<form action="confirm.php" method="post"> 35<table border="0" class="touroku"> 36 37 <tr> 38 <td align="right"> 氏名: </td> 39 <td><input type="text" size="25" name="yourname"></td> 40 </tr> 41 42 <tr> 43 <td align="right"> 都道府県: </td> 44 <td> <select name="live"> 45 <option value="選択してください">選択してください</option> 46 <option value="東京">東京</option> 47 <option value="大阪">大阪</option> 48 <option value="名古屋">名古屋</option> 49 </select> 50 </td> 51 </tr> 52 53 <tr> 54 <td align="right"> 電話番号: </td> 55 <td><input type="text" size="30" name="tel"></td> 56 </tr> 57 58 <tr> 59 <td align="right"> 性別: </td> 60 <td><label><input type="radio" name="gender" value="男性">男性</label> 61 <label><input type="radio" name="gender" value="女性">女性</label> 62 <label><input type="radio" name="gender" value="未回答">未回答</label> 63 </td> 64 </tr> 65 66 <tr> 67 <td align="right"> 趣味:</td> 68 <td> 69 <label><input type="checkbox" value="趣味" name="shumi[1]">映画</label> 70 <label><input type="checkbox" value="趣味" name="shumi[2]">グルメ</label> 71 <label><input type="checkbox" value="趣味" name="shumi[3]">スポーツ<br></label> 72 <label><input type="checkbox" value="趣味" name="shumi[4]">読書</label> 73 <label><input type="checkbox" value="趣味" name="shumi[5]">ファッション</label> 74 <label><input type="checkbox" value="趣味" name="shumi[6]">アニメ</label> 75 </td> 76 </tr> 77 78 <tr> 79 <td align="right"> 備考欄: </td> 80 <td><textarea name="kanso" rows="3" cols="40">感想</textarea></td> 81 </tr> 82 83</table> 84 85<a href="index.html"><input type="button" value="一覧へ戻る"></a> 86<a href="confirm.php"><input type="submit" value="確認する"></a> 87 88 89 90</div> 91</div> 92</div> 93 94<footer> 95 96 97 98 99</footer> 100 101</div> 102 103 104 </body> 105</html>
確認フォーム
php
1<?php 2// $_POSTで受け取る 3$yourname = $_POST["yourname"]; 4$live = $_POST["live"]; 5$tel = $_POST["tel"]; 6$gender = $_POST["gender"]; 7$shumi = $_POST["shumi"]; 8$kanso = $_POST["kanso"]; 9?> 10 11 12 13<!DOCTYPE html> 14<html> 15<head> 16<meta charset="utf-8" /> 17<title>My Web Site</title> 18<link rel="stylesheet" href="style3.css"> 19 20</head> 21<body> 22 23 <div class="main"> 24 <div class="form-title"></div> 25 <h2>確認画面</h2> 26 27 28<table border="0" class="form-item"> 29<tr> 30 <td align="right" class="form-item"> 氏名 : </td> 31 <td align="left"><?php echo $yourname ?></td> 32</tr> 33 34<tr> 35 <td align="right" class="form-item"> 都道府県 : </td> 36 <td align="left"><?php echo $live ?></td> 37</tr> 38 39<tr> 40 <td align="right" class="form-item"> 電話番号 : </td> 41 <td align="left"><?php echo $tel ?></td> 42</tr> 43 44<tr> 45 <td align="right" class="form-item"> 性別 : </td> 46 <td align="left"> 47 <?php 48 if (isset($_POST["gender"])) {//もしPOSTに[genderがあれば 49 if ($_POST["gender"] == "男性") { //もし男性なら 50 echo "男性"; 51 } 52 elseif ($_POST["gender"] == "女性") { //女性なら 53 echo "女性"; 54 } 55 elseif ($_POST["gender"] == "未回答") { //未回答なら 56 echo "未回答"; 57 } 58 else { 59 echo "どれかチェックしてから[送信]ボタンを押してください。"; 60 } 61 } 62 ?> 63 </td> 64</tr> 65 66<tr> 67 <td align="right" class="form-item"> 趣味 : </td> 68 <td align="left"> 69 <?php 70 if (isset($_POST["shumi"])) { //チェックされていれば 71 $shumi = $_POST["shumi"]; //変数 $shumi に格納 72 } 73 if (isset($shumi[1])) { 74 echo " 映画"; 75 } 76 if (isset($shumi[2])) { 77 echo " グルメ"; 78 } 79 if (isset($shumi[3])) { 80 echo " スポーツ"; 81 } 82 if (isset($shumi[4])) { 83 echo " 読書"; 84 } 85 if (isset($shumi[5])) { 86 echo " ファッション"; 87 } 88 if (isset($shumi[6])) { 89 echo " アニメ"; 90 } 91 else { 92 echo ""; 93 } 94 ?> 95 </td> 96</tr> 97 98<tr> 99 <td align="right" class="form-item"> 備考欄 : </td> 100 <td align="left"><?php echo $kanso ?></td> 101</tr> 102</table> 103 104 105<a href="index2.html"><input type="button" name="back" width="30px" value="戻る"></a> 106<a href="registration.php"><input type="submit" name="button1" width="30px" value="送信"></p></a> 107 108 109</div> 110</div> 111 112 113</body> 114</html> 115
PHP
1(index.php データーベースに繋げるための専用のファイル) 2 3<?php 4//DBにMysql、データベース名・testを指定。 5$dsn = 'mysql:dbname=uriage;host=localhost'; 6 7//DBに接続するためのユーザー名・パスワードを設定 8$user = 'andrew'; 9$password = 'andrew135'; 10 11 12try{ 13//データーベースに接続 14 $pdo = new PDO($dsn, $user, $password); 15 16 17} 18 19//接続に失敗した際のエラー処理 20catch (PDOException $e){ 21 print('エラーが発生しました。:'.$e->getMessage()); 22 die(); 23} 24?>
php
1(registration.phpのコード) 2 3<?php 4 5require_once('index_db.php'); 6 7 8try { 9 10//データーベースに接続 11 12 13 //ここに処理を記載 14 $stmt = $pdo->prepare("INSERT INTO member (yourname, live, tel, gender, hobby, shumi) VALUES (:yourname, :live, :tel, :gender, :hobby, :shumi)"); 15 $stmt->bindValue(':yourname', $yourname, PDO::PARAM_STR); 16 $stmt->bindValue(':live', $live, PDO::PARAM_STR); 17 $stmt->bindValue(':tel', $tel, PDO::PARAM_INT); 18 $stmt->bindValue(':gender', $gender, PDO::PARAM_STR); 19 $stmt->bindValue(':shumi', $shumi, PDO::PARAM_STR); 20 $stmt->bindValue(':shumi', $kanso, PDO::PARAM_STR); 21 $stmt->execute(); 22 if($stmt){ 23 echo 'stmt OK!!'; 24 }else{ 25 echo 'stmt NG...'; 26 } 27 //接続終了 28 $pdo = null; 29} 30 31//接続に失敗した際のエラー処理 32catch (Exception $e){ 33 echo 'error'; 34 echo('エラーが発生しました。:'.$e->getMessage()); 35 die(); 36} 37 38 39?> 40<html> 41<head> 42 43<title>OK</title> 44</head> 45<body> 46 47<p>登録完了!</p> 48</body> 49</html> 50 51 52 53
###試したこと
registration.phpの中のtryの前にPOSTで受け取る内容のデーターを入れましたが解決できませんでした。
$yourname = $_POST["yourname"]; $live = $_POST["live"]; $tel = $_POST["tel"]; $gender = $_POST["gender"]; $shumi = $_POST["shumi"]; $kanso = $_POST["kanso"];
###補足情報(言語/FW/ツール等のバージョンなど)
PHP 5.6.30, mysql Ver 14.14
回答3件
あなたの回答
tips
プレビュー