Q&A
皆様いつもお世話になっております。
フォームからPOSTされた値をMySQLにINSERTしたいのです。
以下のテストコードでは成功するのですが、
PHP
1//テストコード 2 3<?php 4function accessSQL(){ 5 $user = "root"; 6 $pass = "root"; 7 $dbh = new PDO('mysql:host=localhost;dbname=todo;charset=utf8', $user, $pass); 8 return $dbh; 9} 10 11function insertSQL($code, $prod_name, $mount, $category, $end_date){ 12 try { 13 $dbh = accessSQL(); 14 $stmt = $dbh->prepare("insert into test (id, code, prod_name, mount, category, end_date) values ('', :code, :prod_name, :mount, :category, :end_date)"); 15 $stmt->bindParam(':code', $code, PDO::PARAM_STR); 16 $stmt->bindParam(':prod_name', $prod_name, PDO::PARAM_STR); 17 $stmt->bindParam(':mount', $mount, PDO::PARAM_INT); 18 $stmt->bindParam(':category', $category, PDO::PARAM_STR); 19 $stmt->bindParam(':end_date', $end_date, PDO::PARAM_STR); 20 $stmt->execute(); 21 $stmt = NULL; 22 }catch (PDOException $e){ 23 echo $e->getMessage(); 24 } 25} 26 27$prod_name = "さばの塩焼き"; 28$code = "FZ44007"; 29$mount = "30"; 30$category = "鮮魚"; 31$year = "2020"; 32$mon = "06"; 33$day = "23"; 34$end_date = $year."-".$mon."-".$day; 35 36insertSQL($code, $prod_name, $mount, $category ,$end_date); //成功する。
このようにテストコードでは成功します。
以下のコードで、フォームからのPOSTの値を関数テストコードと同様のinsertSQLにPOSTの変数と変数を入力すると、INSERTされません。エラーメッセージは出ておりません。
PHP
1<?php 2/*コードの生成*/ 3function makeCode(){ 4 $prod_code = substr(str_shuffle("1234567890"), 0, 5); 5 return $prod_code; 6} 7 8function checkDouble($code){ 9 $dbh = accessSQL(); 10 $stmt = $dbh->prepare("select count(*) from test where code = ?"); 11 $stmt->execute([$code]); 12 $totalCode = (int)$stmt->fetchColumn(); 13 $stmt = NULL; 14 if ($totalCode > 0) { 15 $newCode = makeCode(); 16 //"重複あり"; 17 return $newCode; 18 }else{ 19 return $code; 20 } 21} 22 23$code = "FZ".makeCode(); 24$code = checkDouble($code); 25 26/*コードの生成ここまで*/ 27 28function accessSQL(){ 29 $user = "root"; 30 $pass = "root"; 31 $dbh = new PDO('mysql:host=localhost;dbname=hozon;charset=utf8', $user, $pass); 32 return $dbh; 33} 34 35function insertSQL($code, $prod_name, $mount, $category, $end_date){ 36 try{ 37 $dbh = accessSQL(); 38 $stmt = $dbh->prepare("insert into test (id, code, prod_name, mount, category, end_date) values ('', :code, :prod_name, :mount, :category, :end_date)"); 39 $stmt->bindParam(':code', $code, PDO::PARAM_STR); 40 $stmt->bindParam(':prod_name', $prod_name, PDO::PARAM_STR); 41 $stmt->bindParam(':mount', $mount, PDO::PARAM_INT); 42 $stmt->bindParam(':category', $category, PDO::PARAM_STR); 43 $stmt->bindParam(':end_date', $end_date, PDO::PARAM_STR); 44 $stmt->execute(); 45 $stmt = NULL; 46 }catch (PDOException $e){ 47 echo $e->getMessage(); 48 } 49} 50 51if (isset($_POST["submit"])){ 52 $end_date = $_POST["year"]."-".$_POST["mon"]."-".$_POST["day"]; 53 insertSQL($code, $_POST["prod_name"], $_POST["mount"], $_POST["category"], $end_date); //insertに失敗する 54 //insertSQL('FZ76587','赤み', 30, '鮮魚', 2020-10-30); //insertに失敗する 55} 56?> 57<!doctype html> 58<html lang="ja"> 59<head> 60 <meta charset="UTF-8"> 61 <meta name="viewport" 62 content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> 63 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 64 <title>Document</title> 65</head> 66<body> 67 68<form action="index.php" method="post"> 69 <input type="text" name="prod_name" placeholder="商品名"> 70 <input type="text" name="mount" placeholder="個数"> 71 <select name="category"> 72 <option value="鮮魚">鮮魚</option> 73 <option value="冷凍食品">冷凍食品</option> 74 <option value="常温">常温</option> 75 </select> 76 <input type="text" name="year" placeholder="年">年 77 <input type="text" name="mon" placeholder="月">月 78 <input type="text" name="day" placeholder="日">日 79 <input type="submit" name="submit" value="登録"> 80</form> 81 82</body> 83</html>
テーブルの構造は以下の通りです。
この場合、どこにINSERTに失敗する原因があるのでしょうか?アドバイスとご教授のほどよろしくお願いします。
回答1件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。