こちらのコード、 cust_id取得の部分がまだ解決していないので途中コードがおかしいですが、先生にhiddenの部分はなぜ存在するのか?hiddenというのはバグを作っているようなものだ。もしこれが無ければプログラムが動かないなら、他に問題があるはずだ。
と言われました。
php
1 2<?php 3/* Program name: register.php 4 * Description: Program displays the blank form and checks 5 * all the form fields for blank fields. 6 */ 7 8ini_set("display_errors","on"); 9error_reporting(E_ALL | E_STRICT); 10ini_set("include_path","./includes"); 11include("dbinfo.inc"); 12$gen="Gender"; 13$rows = 0; 14$last_id = 0; 15 16 17if(isset($_POST['submitted']) and $_POST['submitted'] == "yes") 18{ 19 if(!isset($_POST['gen'])) 20 { 21 $blank_data[] = $gen; 22 } 23 foreach($_POST as $field => $value) 24 { 25 if(empty($value)) 26 { 27 $blank_data[] = $field; 28 } 29 else 30 { 31 $good_data[$field] = strip_tags(trim($value)); 32 } 33 } 34 if(@sizeof($blank_data) > 0) 35 { 36 $message = "<p style='color: red; margin-bottom: 0; 37 font-weight: bold'> 38 You didn't fill in one or more required fields. 39 You must enter: 40 <ul style='color: red; margin-top: 0; 41 list-style: none' >"; 42 /* display list of missing information */ 43 foreach($blank_data as $value) 44 { 45 $message .= "<li>$value</li>"; 46 } 47 $message .= "</ul>"; 48 //extract($good_data); 49 echo $message; 50 include("form.inc"); 51 exit(); 52 } 53 foreach($_POST as $field => $value) 54 { 55 if(!empty($value)) 56 { 57 //validations 58 $name_patt = "/^[A-Za-z0-9' -]{1,15}$/"; 59 $email_patt = "/^.+@.+\\..+$/"; 60 $phone_patt = "/^[0-9)(xX -]{7,20}$/"; 61 $passwd_patt = "/^[A-Za-z0-9' -]{1,60}$/"; 62 63 $field = htmlspecialchars($field, ENT_QUOTES); 64 $value = htmlspecialchars($value, ENT_QUOTES); 65 66 if(preg_match("/username/i",$field)) 67 { 68 if(!preg_match($name_patt,$value)) 69 { 70 $error_array[] = "$value is not a valid Username"; 71 } 72 } 73 if(preg_match("/phone/i",$field)) 74 { 75 if(!preg_match($phone_patt,$value)) 76 { 77 $error_array[] = "$value is not a valid Phone"; 78 } 79 } 80 if(preg_match("/email/i",$field)) 81 { 82 if(!preg_match($email_patt,$value)) 83 { 84 $error_array[] = "$value is not a valid Email"; 85 } 86 } 87 88 if(preg_match("/password/i",$field)) 89 { 90 if(!preg_match($passwd_patt,$value)) 91 { 92 $error_array[] = "$value is not a valid password"; 93 } 94 } 95 echo "<div class='field'>"; 96 } 97 } 98 99 if(@sizeof($error_array) > 0) 100 { 101 $message = "<ul style='color: red; list-style: none' >"; 102 foreach($error_array as $value) 103 { 104 $message .= "<li>$value</li>"; 105 } 106 $message .= "</ul>"; 107 echo $message; 108 @extract($clean_data); 109 include("form.inc"); 110 exit(); 111 } 112 else 113 { 114 $cxn = mysqli_connect($host,$user,$passwd,$dbname) 115 or die("Couldn't connect to server"); 116 $clean_data = array(); 117 foreach($_POST as $field => $value) 118 { 119 $clean_data[$field] = mysqli_real_escape_string($cxn,$value); 120 } 121 $date = date("Y-m-d H:i:s"); 122 ?> 123 124 <form method="post"> 125 <input type="hidden" name='$field' value="<?php echo htmlspecialchars($good_data['value']); ?>"> 126 <input type="hidden" name="gen" value="<?php echo $gen; ?>"> 127 <input type="hidden" name="date" value="<?php echo $date; ?>"> 128 </form> 129 130 <?php 131 if (!empty($good_data['username']) ) { // Confirm the username on the DB that user input 132 //$sql = "SELECT username FROM login"; 133 $sql = "SELECT username FROM login WHERE username = " . $good_data['username']; 134 if ($result = $cxn->query($sql)) { 135 $username_result = $good_data['username']; 136 $rows =1; 137 } 138 if ($rows == 1) 139 { 140 echo "Username already exists"; 141 include("form.inc"); 142 exit; 143 } else { 144 145 146 /* 接続状況をチェックします */ 147 if (mysqli_connect_errno()) { 148 printf("Connect failed: %s\n", mysqli_connect_error()); 149 exit(); 150 } 151 mysqli_query($result, "CREATE TABLE login LIKE cust_ID"); 152 153 154 /* loop that displays the form */ 155 $sql = "INSERT INTO login (username,phone,email,password,gen,date) 156 VALUES ('".$clean_data['username']."', 157 '".$clean_data['phone']."', 158 '".$clean_data['email']."', 159 '".$clean_data['password']."', 160 '".$clean_data['gen']."', 161 '$date')"; 162 } 163 $result = mysqli_query($cxn,$sql) 164 or die("Couldn't execute query1"); 165 $sql = "SELECT LAST_INSERT_ID() AS id;"; 166 $newID = mysqli_query($cxn,$sql) or die("Couldn't execute query"); 167 168 print_r($newID); 169 170 171 printf ("New Record has id %d.\n", mysqli_insert_id($result)); 172 173 /* 接続を閉じます */ 174 mysqli_close($sql); 175 176 177/*$sql = "SELECT LAST_INSERT_ID() AS id;"; 178 $newID = mysqli_query($cxn,$sql) or die("Couldn't execute query"); 179 180 print_r($newID);*/ 181 182 $signUpMessage = 183 ' <br>Username is '.$clean_data['username'].' <br>'. 184 ' Phone is '.$clean_data['phone'].' <br>'. 185 ' Email is '.$clean_data['email'].' <br>'. 186 ' Gender is '.$clean_data['gen'].' <br>'. 187 ' The registration date is '.$date.' <br>'. 188 ' Keep your password for your security.'; 189 echo $signUpMessage; 190 include("stored.inc"); 191 } 192 } 193} 194else 195{ 196 include("form.inc"); 197} 198?> 199
このコードのhiddenの部分です。
php
1<form method="post"> 2 <input type="hidden" name='$field' value="<?php echo htmlspecialchars($good_data['value']); ?>"> 3 <input type="hidden" name="gen" value="<?php echo $gen; ?>"> 4 <input type="hidden" name="date" value="<?php echo $date; ?>"> 5 </form>
しかし、この先生、@については、かなり推奨していて、とてもusefulだと言います。
@は良くて、hiddenがダメな理由は何でしょうか。
また、実際にはこのhiddenの部分に一度データを入れないとインプットされたデータが表示されません。
もしこのhiddenがダメなら、何に変えればいいでしょうか。
追加
回答者さまから、質問の内容がわからないとのことなので、追加させていただこうと思います。
⚪️ @の方がよほどバグを隠すはずなのに、なぜ先生は@を積極的に使うのか、
⚪️ そしてhiddenのコードの箇所はなぜバグを生むと言っているのか、
この2点をお伺いしたいです。特に2番目です。
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/26 02:13