PHPにて、PHPMyAdminから取得してきた値をラジオボタンで選択し、送信したのですが毎回同じ値が送られてきてしまいます。
以下が僕が作成したコードになります。
php
1<?php 2date_default_timezone_set('Asia/Tokyo'); 3const HOST = ''; 4const DB_NAME = ''; 5const USER_NAME = ''; 6const PASSWORD = ''; 7$err_msg = array(); 8$change = 0; 9$drink_name = ''; 10$date = date('Y/m/d H:i:s'); 11$drink_info_list = array(); 12 13if(($link = mysqli_connect(HOST,USER_NAME,PASSWORD,DB_NAME)) !== FALSE){ 14 mysqli_set_charset($link,'UTF8'); 15 16 17 if($_SERVER['REQUEST_METHOD'] === 'POST') { 18 $stock_numbers = (int)$_POST['stock_numbers']; 19 $drink_id = (int)$_POST['drink_id']; 20 $selected_drink = $_POST['selected_drink']; 21 $amount_money = trim(mb_convert_kana($_POST['amount_money'],"s",'UTF-8')); 22 $amount_money_length = mb_strlen($amount_money, "UTF-8"); 23 $amount_money_width = mb_strwidth($amount_money, "UTF-8");//不要では 24 25 //入力値チェック 26 if($selected_drink === NULL){ 27 $err_msg[] = 'ドリンクを指定して下さい'; 28 } 29 30 if(empty($amount_money) === TRUE){ 31 $err_msg[] = '金額を入力してください'; 32 } 33 if($amount_money_length === $amount_money_width){ 34 if(empty($amount_money) === FALSE){ 35 if(ctype_digit($amount_money) === FALSE){ //ctype_digit:string型以外はfalseになる。 36 $err_msg[] = '金額は整数で入力してください'; 37 } 38 } 39 }else{ 40 $err_msg[] = '金額は半角で入力して下さい'; 41 42 43 } 44 45 46 47 48 49 $amount_money = (int)$amount_money; 50 51 52 //var_dump($amount_money);exit(); 53 54 55 56 //エラーメッセージが空だった場合 57 if (count($err_msg) === 0){ 58 $sql = 'SELECT drink_name,price FROM drink_info_table WHERE drink_id = ' . $drink_id; 59 60 if($result = mysqli_query($link,$sql)){ 61 while($row = mysqli_fetch_assoc($result)){ //while文について質問。 62 63 $price = htmlspecialchars($row['price'],ENT_QUOTES, 'UTF-8'); 64 $drink_name = htmlspecialchars($row['drink_name'],ENT_QUOTES, 'UTF-8'); 65 var_dump($drink_id);exit(); 66 } 67 }else{ 68 $err_msg[] = 'drink_info_table:SELECTエラー:'.$sql; 69 } 70 71 72 if($amount_money < $price){ 73 $err_msg[] = '金額が足りません'; 74 } 75 if($amount_money > $price){ 76 $change = $amount_money - $price; 77 } 78 79 } 80 //エラーがあってもUPDATEするの?? 81 if (count($err_msg) === 0){ 82 $sql = 'UPDATE drink_stock_table SET stock_number = ' . $stock_numbers . '- 1 WHERE drink_id = ' . $drink_id; 83 if(mysqli_query($link,$sql) === FALSE){ 84 $err_msg[] = 'drink_stock_table: UPDATEエラー' . $sql; 85 } 86 $sql = 'INSERT INTO drink_history_table(drink_id,bought_date) VALUES (' . $drink_id . ',\''. $date .'\')'; 87 if(mysqli_query($link,$sql) === FALSE){ 88 $err_msg[] = 'drink_history_table: insertエラー' . $sql; 89 } 90 } 91 92 } 93}else{ 94 $err_msg[] = 'DB接続失敗'; 95} 96 97 98 99?> 100 101 102<!DOCTYPE html> 103<html lang="ja"> 104<head> 105 <meta charset="UTF-8"> 106 <title></title> 107</head> 108<body> 109 <h1>自動販売機結果</h1> 110 <?php foreach($err_msg as $value){ ?> 111 <p><?php echo $value; ?></p> 112 <?php } ?> 113 <?php if(count($err_msg) === 0){?> 114 <img src="img/<?php echo $drink_id ;?>.pic"> 115 <p>がしゃん!【<?php echo $drink_name; ?>】が買えました! </p> 116 <?php } ?> 117 <?php if($change >= 1){ ?> 118 <p>おつりは【<?php echo $change; ?>円】です</p> 119 <?php } ?> 120 <footer> 121 <a href="index.php">戻る</a> 122 </footer> 123</body> 124</html>
上記のコードは選択された値の送られ先となります。(result.php)
下記が値を選択し、送る側のコードとなります。(index.php)
php
1<?php 2const HOST = ''; 3const DB_NAME = ''; 4const USER_NAME = ''; 5const PASSWORD = ''; 6$drink_info_list = array(); 7 8//DB接続 9if(($link = mysqli_connect(HOST,USER_NAME,PASSWORD,DB_NAME)) !== FALSE){ 10 mysqli_set_charset($link,'UTF8'); 11 // sql where にて 以下の条件を追加すること。 12 // 1.ステータスが公開になっているもののみを表示。 13 // 2.在庫数が1以上の商品のみ。 14 $sql = 'SELECT drink_info_table.drink_id,drink_name,price,status,stock_number 15 FROM drink_info_table 16 JOIN drink_stock_table 17 ON drink_info_table.drink_id = drink_stock_table.drink_id 18 WHERE status = 1 AND stock_number >= 1;'; 19 20 21 if($result = mysqli_query($link,$sql)){ 22 $i = 0; 23 while($row = mysqli_fetch_assoc($result)){ 24 $drink_info_list[$i]['drink_id'] = htmlspecialchars($row['drink_id'],ENT_QUOTES, 'UTF-8'); 25 $drink_info_list[$i]['drink_name'] = htmlspecialchars($row['drink_name'],ENT_QUOTES, 'UTF-8'); 26 $drink_info_list[$i]['price'] = htmlspecialchars($row['price'],ENT_QUOTES, 'UTF-8'); 27 $drink_info_list[$i]['status'] = htmlspecialchars($row['status'],ENT_QUOTES, 'UTF-8'); 28 $drink_info_list[$i]['stock_number'] = htmlspecialchars($row['stock_number'],ENT_QUOTES, 'UTF-8'); 29 30 $i++; 31 } 32 33 // var_dump($drink_info_list);exit(); 34 } 35 36 37 38} 39 40?> 41 42 43<!DOCTYPE html> 44<html lang="ja"> 45<head> 46 <meta charset="UTF-8"> 47 <title></title> 48</head> 49<body> 50 <h1>自動販売機</h1> 51 52 <form action="result.php" method="post"> 53 <div>金額<input type="text" name="amount_money"></div> 54 <!--画像を表示すること--> 55 <?php foreach($drink_info_list as $values){ ?> 56 <img src="img/<?php echo $values['drink_id'] ;?>.pic"> 57 <?php echo $values['drink_name']; ?> 58 <?php echo $values['price']; ?> 59 60 <?php if((int)$values['stock_number'] === 0){ ?> 61 <p>売り切れ</p> 62 <?php } else { ?> 63 <input type="radio" name="selected_drink"> 64 <?php } ?> 65 66 <?php } ?> 67 68 <p><input type="submit" value="■□■□■購入■□■□■"></p> 69 <input type="hidden" name="stock_numbers" value="<?php echo $values['stock_number']; ?>"> 70 <input type="hidden" name="drink_id" value="<?php echo $values['drink_id'];?>"> 71 72 </form> 73 74</body> 75</html>
「同じ値」ということなんですが、「result.php」の
$drink_name = htmlspecialchars($row['drink_name'],ENT_QUOTES, 'UTF-8');の下の行でvar_dumpした所,
var_dump($drink_id);exit();選択肢を変えても、毎回同じ「$drink_id」が送られてきてしまいます。
下記はindex.phpの実行画面となります。
金額を入力し、どの商品を選択しても必ずコーラが購入されてしまいます。
ちなみになのですが、$drink_idでvar_dumpした所を、$drink_nameにしたら文字化してしまい、判定できませんでした...
どなたか、教えて下さる方いましたらご教授願います...
回答3件
あなたの回答
tips
プレビュー