Undefined indexエラーが起きていますが、formで送った値の名前と
受け取る値の名前は同一になっていますので、なぜこのエラーが吐き出されているのかわかりません。
アプリの概要:form.htmlで値を入力し、add.phpで処理して、DBに入力内容を登録するといったものです
先輩方のご助力をいただければと思います。
よろしくお願いします
□form.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>入力フォーム</title> </head> <body> 入力フォーム<br> <form action="add.php" method="post"> 料理名:<input type="text" name="recipe_name" required><br> カテゴリ: <select name="category"> <option value="">選択してください</option> <option value="1">和食</option> <option value="2">中華</option> <option value="3">洋食</option> </select> <br> 難易度: <input type="radio" name="difficulty" value="1">簡単 <input type="radio" name="difficulty" value="2">普通 <input type="radio" name="difficulty" value="3" checked>難しい <br> 予算:<input type="number" min="1" max="9999" name="budget">円 <br> 作り方: <textarea name="howto" cols="40" rows="4" maxlength="150"></textarea> <input type="submit" value="送信"> </form> </body> </html>
□add.php <?php $user="people"; $pass="password"; $recipe_name = $_POST['recipe_name']; $howto = $_POST['howto']; $category=(int) $_POST['category']; $difficulty = (int) $_POST['difficulty']; $budget=(int) $_POST['budget']; try{ $dbh=new PDO('mysql:host=localhost;dbname=db1',$user, $pass); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $sql="INSERT INTO recipes (recipe_name,category,difficulty,budget,howto) VALUES(?,?,?,?,?)"; $stmt=$dbh->prepare($sql); $stmt->bindValue(1,$recipe_name,PDO::PARAM_STR); $stmt->bindValue(2,$category,PDO::PARAM_INT); $stmt->bindValue(3,$difficulty,PDO::PARAM_INT); $stmt->bindValue(5,$budget,PDO::PARAM_INT); $stmt->bindValue(6,$recipe_name,PDO::PARAM_STR); $stmt->execute(); $dbh=null; echo"レシピの登録が完了しました"; }catch(PDOException $e){ echo"エラー発生:" . htmlspecialchars($e->getMessage(),ENT_QUOTES,'UTF-8') . "<br>"; die(); } ?>
「以下、エラーメッセージ」
[01-Mar-2018 13:36:16 UTC] PHP Notice: Undefined index: recipe_name in /Applications/MAMP/htdocs/php/add.php on line 5
[01-Mar-2018 13:36:16 UTC] PHP Notice: Undefined index: difficulty in /Applications/MAMP/htdocs/php/add.php on line 8