現在、以下のようなプログラムになっています。ユーザーがデータを登録するとDB側で自動的にcust_IDが割り振られるようになっています。
php
1<?php 2/* Program name: register.php 3 * Description: Program displays the blank form and checks 4 * all the form fields for blank fields. 5 */ 6 7ini_set("display_errors","on"); 8error_reporting(E_ALL | E_STRICT); 9ini_set("include_path","./includes"); 10include("dbinfo.inc"); 11$gen="Gender"; 12$rows = 0; 13 14 15if(isset($_POST['submitted']) and $_POST['submitted'] == "yes") 16{ 17 if(!isset($_POST['gen'])) 18 { 19 $blank_data[] = $gen; 20 } 21 foreach($_POST as $field => $value) 22 { 23 if(empty($value)) 24 { 25 $blank_data[] = $field; 26 } 27 else 28 { 29 $good_data[$field] = strip_tags(trim($value)); 30 } 31 } 32 if(@sizeof($blank_data) > 0) 33 { 34 $message = "<p style='color: red; margin-bottom: 0; 35 font-weight: bold'> 36 You didn't fill in one or more required fields. 37 You must enter: 38 <ul style='color: red; margin-top: 0; 39 list-style: none' >"; 40 /* display list of missing information */ 41 foreach($blank_data as $value) 42 { 43 $message .= "<li>$value</li>"; 44 } 45 $message .= "</ul>"; 46 //extract($good_data); 47 echo $message; 48 include("form.inc"); 49 exit(); 50 } 51 foreach($_POST as $field => $value) 52 { 53 if(!empty($value)) 54 { 55 //validations 56 $name_patt = "/^[A-Za-z0-9' -]{1,15}$/"; 57 $email_patt = "/^.+@.+\\..+$/"; 58 $phone_patt = "/^[0-9)(xX -]{7,20}$/"; 59 $passwd_patt = "/^[A-Za-z0-9' -]{1,60}$/"; 60 61 $field = htmlspecialchars($field, ENT_QUOTES); 62 $value = htmlspecialchars($value, ENT_QUOTES); 63 64 if(preg_match("/username/i",$field)) 65 { 66 if(!preg_match($name_patt,$value)) 67 { 68 $error_array[] = "$value is not a valid Username"; 69 } 70 } 71 if(preg_match("/phone/i",$field)) 72 { 73 if(!preg_match($phone_patt,$value)) 74 { 75 $error_array[] = "$value is not a valid Phone"; 76 } 77 } 78 if(preg_match("/email/i",$field)) 79 { 80 if(!preg_match($email_patt,$value)) 81 { 82 $error_array[] = "$value is not a valid Email"; 83 } 84 } 85 86 if(preg_match("/password/i",$field)) 87 { 88 if(!preg_match($passwd_patt,$value)) 89 { 90 $error_array[] = "$value is not a valid password"; 91 } 92 } 93 echo "<div class='field'>"; 94 } 95 } 96 97 if(@sizeof($error_array) > 0) 98 { 99 $message = "<ul style='color: red; list-style: none' >"; 100 foreach($error_array as $value) 101 { 102 $message .= "<li>$value</li>"; 103 } 104 $message .= "</ul>"; 105 echo $message; 106 @extract($clean_data); 107 include("form.inc"); 108 exit(); 109 } 110 else 111 { 112 $cxn = mysqli_connect($host,$user,$passwd,$dbname) 113 or die("Couldn't connect to server"); 114 $clean_data = array(); 115 foreach($_POST as $field => $value) 116 { 117 $clean_data[$field] = mysqli_real_escape_string($cxn,$value); 118 } 119 $date = date("Y-m-d H:i:s"); 120 ?> 121 122 <form method="post"> 123 <input type="hidden" name='$field' value="<?php echo htmlspecialchars($good_data['value']); ?>"> 124 <input type="hidden" name="gen" value="<?php echo $gen; ?>"> 125 <input type="hidden" name="date" value="<?php echo $date; ?>"> 126 </form> 127 128 <?php 129 if (!empty($good_data['username']) ) { // Confirm the username on the DB that user input 130 131 $sql = "SELECT username FROM login WHERE username = '" . $good_data['username']; 132 if ($result = $cxn->query($sql)) { 133 $username_result = $good_data['username']; 134 $rows =1; 135 } 136 if ($rows == 1) 137 { 138 echo "Username already exists"; 139 include("form.inc"); 140 exit; 141 } else { 142 143 144 /* loop that displays the form */ 145 $sql = "INSERT INTO login (username,phone,email,password,gen,date) 146 VALUES ('".$clean_data['username']."', 147 '".$clean_data['phone']."', 148 '".$clean_data['email']."', 149 '".$clean_data['password']."', 150 '".$clean_data['gen']."', 151 '$date')"; 152 153 } 154 $result = mysqli_query($cxn,$sql) 155 or die("Couldn't execute query"); 156 $signUpMessage = 157 'Your Username is '.$clean_data['username'].' <br>'. 158 ' Phone is '.$clean_data['phone'].' <br>'. 159 ' Email is '.$clean_data['email'].' <br>'. 160 ' Gender is '.$clean_data['gen'].' <br>'. 161 ' The registration date is '.$date.' <br>'. 162 ' Keep your password for your security.'; 163 echo $signUpMessage; 164 include("stored.inc"); 165 } 166 } 167} 168else 169{ 170 include("form.inc"); 171} 172?> 173
そのcust_IDをSELECT文で取得したいのですが、今まで登場してこなかったcust_IDなので、Undefine errorになってしまいます。
現在、このようになってるところを
php
1$sql = "SELECT username FROM login WHERE username = '" . $good_data['username'];
このように直してみました。
php
1$sql = "SELECT cust_ID, username FROM login WHERE username = '" . $good_data['cust_ID']. $good_data['username'];
インプットされたデータではなく、DB側で自動発行されたcust_IDをどのようにすれば取得できるでしょうか。
テーブルはこのようになっています。
テーブル名: login
cust_ID INT(5) AUTO_INCREMENT.Primary key.Unique.
username VARCHAR(15)
email VARCHAR(50)
password VARCHAR(60)
date datetime
テーブル名: login_time
login_no INT(5)Primary key.
cust_ID INT(5)Primary key.
logtime TIMESTAMP
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/25 05:29
2017/05/25 05:57 編集
2017/05/25 06:18
2017/05/25 07:52
2017/05/25 08:11
2017/05/25 08:24
2017/05/25 14:56
2017/05/26 00:37
2017/05/26 01:00
2017/05/26 01:42
2017/05/26 01:45
2017/05/26 02:30
2017/05/26 03:22
2017/05/26 05:20