質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

1回答

1063閲覧

php_エラーメッセージの出力

pomi

総合スコア10

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2017/03/07 06:48

名前欄”name”に入力がなかった場合、
”ユーザー名を入力してください”
エラーメッセージを出力させたいと思っています。

if($statement->execute())
の条件文にelseを挿入。

ちなみにその状態でのエラー文は下記の状態になっています
Notice: Undefined variable: rows in /Applications/XAMPP/xamppfiles/htdocs/phpkiso/DB/DB_search.php on line 88

Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/phpkiso/DB/DB_search.php on line 88

恐らく名前欄"name"がnullであっても,
このelseフラグには入ってないと思われます。

飛んでエラー文は88行、

<?php foreach($rows as $row){ ?>

試行錯誤したのですが原因がわからず、
質問作せて頂きました。

よろしくお願い致します。

php

1<!DOCTYPE html> 2<html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>顧客管理*検索</title> 6 <link rel="stylesheet" href="DB_color.css" class="e"> 7 <form action="DB_top.php" method="post"> 8 </head> 9 <body background="back.gif" bgcolor="#ccc" class="a"> 10 <h1>顧客情報 検索画面</h1> 11 <?php 12 $hurigana = $_POST['hurigana']; 13 $name = $_POST['name']; 14 $sex = $_POST['sex']; 15 $post1 = $_POST['post1']; 16 $post2 = $_POST['post2']; 17 $adress = $_POST['adress']; 18 $tell = $_POST['tell']; 19 $email = $_POST['email']; 20 $birth = $_POST['birth']; 21 $birth_m = $_POST['birth_m']; 22 $birth_d = $_POST['birth_d']; 23 $hour = $_POST['hour']; 24 $minute = $_POST['minute']; 25 $year1 = $_POST['year1']; 26 $year2 = $_POST['year2']; 27 $year3 = $_POST['year3']; 28 $year01 = $_POST['year01']; 29 $year02 = $_POST['year02']; 30 $year03 = $_POST['year03']; 31 $syoukai = $_POST['syoukai']; 32 33 $dsn = 'mysql:host=localhost;dbname=customer;charset=utf8'; 34 $user = 'root'; 35 $password = ''; 36 37 $dbh = new PDO($dsn, $user, $password); 38 $dbh->query('SET NAMES utf8'); 39 $sqlcmd = "SELECT * FROM `customer_member` 40 WHERE name like '%$name%' 41 and hurigana like '%$hurigana%' 42 and sex like '%$sex%' 43 and post1 like '%$post1%' 44 and post2 like '%$post2%' 45 and adress like '%$adress%' 46 and tell like '%$tell%' 47 and email like '%$email%' 48 and birth like '%$birth%' 49 and birth_m like '%$birth_m%' 50 and birth_d like '%$birth_d%' 51 and hour like '%$hour%' 52 and minute like '%$minute%' 53 and year1 like '%$year1%' 54 and year2 like '%$year2%' 55 and year3 like '%$year3%' 56 and year01 like '%$year01%' 57 and year02 like '%$year02%' 58 and year03 like '%$year03%' 59 and syoukai like '%$syoukai%'"; 60 61 $statement = $dbh->prepare($sqlcmd); 62 $statement->execute(); 63 $rows; 64 65 if($statement->execute()) 66 { 67 $row_count = $statement->rowCount(); 68// if(!($row_count)) 69 if($row_count == 0) 70 { 71// if(empty($row['name'])); 72// { 73 $ErrorTest = "ユーザー名を入力してください"; 74// } 75 } 76 77 while($row = $statement->fetch()) 78 { 79 $rows[] = $row; 80 } 81 } 82 else 83 { 84 //仮定する 85 $ErrorTest =""; 86 //$_POSTの中がnullの場合 87 if(!empty($row)) 88 { 89 if(empty($row['name'])); 90 { 91 $ErrorTest = "ユーザー名を入力してください"; 92 } 93 } 94 $dbh = null; 95 } 96 ?> 97 <input type="button" onclick="location.href='DB_top.php'" value="トップ戻る" name="botton1 "class="main"> 98 <?php foreach($rows as $row){ ?> 99 <fieldset> 100 <table id="main-category"> 101 <tr> 102 <th>フリガナ</th> 103 <td><input type="text" value="<?php print $row['hurigana']; ?>" class="d-na"></td> 104 </tr> 105 <tr> 106 <th>名 前</th> 107 <td><input type="text" value="<?php print $row['name']; ?>" class="d-na"></td> 108 </tr> 109 <tr> 110 <th>性別</th> 111 <td><input type="text" value="<?php print $row['sex']; ?>" class="d-na"></td> 112 </tr> 113 <tr> 114 <th>郵便番号</th> 115 <td> 116 <input type="text" value="<?php print $row['post1']; ?>" class="d-i">- 117 <input type="text" value="<?php print $row['post2']; ?>" class="d-year"> 118 </td> 119 </tr> 120 <tr> 121 <th>住所</th> 122 <td><input type="text" value="<?php print $row['adress']; ?>" class="d-ad"></td> 123 </tr> 124 <tr> 125 <th>電話番号</th> 126 <td><input type="text" value="<?php echo $row['tell']; ?>" class="d-na"></td> 127 </tr> 128 <tr> 129 <th>E-MAIL</th> 130 <td><input type="text" value="<?php echo $row['email']; ?>" class="d-na"></td> 131 </tr> 132 <tr> 133 <th>生年月日</th> 134 <td> 135 <input type="text" value="<?php echo $row['birth']; ?>" class="d-year">136 <input type="text" value="<?php echo $row['birth_m']; ?>" class="d-i">137 <input type="text" value="<?php echo $row['birth_d']; ?>" class="d-i">138 <input type="text" value="<?php echo $row['hour']; ?>" class="d-i">139 <input type="text" value="<?php echo $row['minute']; ?>" class="d-i">140 </td> 141 </tr> 142 <tr> 143 <th>入 会 日 </th> 144 <td> 145 <input type="text" value="<?php echo $row['year1']; ?>" class="d-y">146 <input type="text" value="<?php echo $row['year2']; ?>" class="d-da">147 <input type="text" value="<?php echo $row['year3'] ;?>" class="d-da">148 </td> 149 </tr> 150 <tr> 151 <th>退 会 日 </th> 152 <td> 153 <input type="text" value="<?php echo $row['year01']; ?>" class="d-y">154 <input type="text" value="<?php echo $row['year02']; ?>" class="d-da">155 <input type="text" value="<?php echo $row['year03'] ;?>" class="d-da">156 </td> 157 </tr> 158 <tr> 159 <th>紹 介 者</th> 160 <td><input type="text" value="<?php print $row['syoukai']; ?>" class="d-na"></td> 161 <br> 162 </fieldset> 163 <?php }?> 164 </table> 165 <input type="button" onclick="location.href='DB_top.php'" value="トップ戻る" name="botton1" class="main"> 166 </body> 167</html> 168 169コード

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

s8_chu

2017/03/07 06:52

formタグの位置は転機ミスでしょうか?
pomi

2017/03/07 07:09

失礼いたしました。
guest

回答1

0

このコードと説明からだと、何をやりたいのか皆目見当がつきませんが、
いくつか言えることとして、

名前欄”name”に入力がなかった場合、
”ユーザー名を入力してください”
エラーメッセージを出力させたいと思っています。

これは、DBを見に行く前に出すものだと思いますよ。

また$row_count = $statement->rowCount();の部分ですが、
rowCountSELECTが返す行数ではなく、INSERTDELETEUPDATEが作用した行数になります。
http://php.net/manual/ja/pdostatement.rowcount.php

もう少し、
入力 → チェック → データの取得 → 表示
という流れをしっかり掴んだほうがいいと思います。

投稿2017/03/07 07:41

shi_ue

総合スコア4437

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問