###前提・実現したいこと
入力フォームからmysqlに登録をしました。
画像の「shumi」の列に関してですが
なぜ「Array」と表示されているのでしょうか?
「shumi」はチェックボタンからデータを受け取りDBに登録した後、
確認したら「Array」と表示されていました。
入力フォーム
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</form> 88 89 90</div> 91</div> 92</div> 93 94<footer> 95 96 97 98 99</footer> 100 101</div> 102 103 </body> 104</html> 105
確認フォーム
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<form action="registration.php" method="post"> 105<input type="hidden" name="yourname" value="<?php echo $yourname; ?>"> 106<input type="hidden" name="live" value="<?php echo $live; ?>"> 107<input type="hidden" name="tel" value="<?php echo $tel; ?>"> 108<input type="hidden" name="gender" value="<?php echo $gender; ?>"> 109<input type="hidden" name="shumi" value="<?php echo $shumi; ?>"> 110<input type="hidden" name="kanso" value="<?php echo $kanso; ?>"> 111<a href="index2.html"><input type="button" name="back" width="30px" value="戻る"></a> 112<a href="registratio.php"><input type="submit" name="button1" width="30px" value="送信"></p></a> 113</form> 114 115</div> 116</div> 117 118 119</body> 120</html> 121
登録フォーム
php
1<?php 2 3require_once('index_db.php'); 4 5 6// $_POSTで受け取る 7$yourname = $_POST["yourname"]; 8$live = $_POST["live"]; 9$tel = $_POST["tel"]; 10$gender = $_POST["gender"]; 11$shumi = $_POST["shumi"]; 12$kanso = $_POST["kanso"]; 13 14 15try { 16 17//データーベースに接続 18$yourname = $_POST['yourname']; 19$live = $_POST['live']; 20$tel = $_POST['tel']; 21$gender = $_POST['gender']; 22$shumi = $_POST['shumi']; 23$kanso = $_POST['kanso']; 24 25 //ここに処理を記載 26 $stmt = $pdo->prepare("INSERT INTO member (yourname, live, tel, gender, shumi, kanso) VALUES (:yourname, :live, :tel, :gender, :shumi, :kanso)"); 27 $stmt->bindValue(':yourname', $yourname, PDO::PARAM_STR); 28 $stmt->bindValue(':live', $live, PDO::PARAM_STR); 29 $stmt->bindValue(':tel', $tel, PDO::PARAM_STR); 30 $stmt->bindValue(':gender', $gender, PDO::PARAM_STR); 31 $stmt->bindValue(':shumi', $shumi, PDO::PARAM_STR); 32 $stmt->bindValue(':kanso', $kanso, PDO::PARAM_STR); 33 $stmt->execute(); 34 var_dump($_POST['yourname']); 35 var_dump($_POST['live']); 36 var_dump($_POST['tel']); 37 var_dump($_POST['gender']); 38 var_dump($_POST['shumi']); 39 var_dump($_POST['kanso']); 40 41 //接続終了 42 $pdo = null; 43} 44 45//接続に失敗した際のエラー処理 46catch (Exception $e){ 47 echo 'error'; 48 echo('エラーが発生しました。:'.$e->getMessage()); 49 exit; 50} 51 52 53?> 54<html> 55<head> 56 57<title>OK</title> 58</head> 59<body> 60 61<p>登録完了!</p> 62</body> 63</html> 64
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/18 14:47
2017/08/18 14:49
2017/08/18 15:32
2017/08/18 15:47
2017/08/19 00:39
2017/08/19 05:41
2017/08/19 08:40