現在レシピサイトをPHPで作っており、登録画面からレシピを入力し確認画面で登録ボタンを押したらデータベースへデータが挿入され登録できるという機能を実装しております。ただレシピの名前と難易度作り方はreciepテーブル、その材料と数量はmaterialテーブルで2つに分けて制作しているのですが、二つのテーブルが紐付けできません。
日本語がおかしかったりコードが変なところがあるとは思いますがご教授いただけないでしょうか?
よろしくお願いします。
【やりたいこと】
recipeテーブルとmaterialテーブルを紐つけて管理したい。
recipeテーブル上のid 1がtitleカツ丼するならば
カツ丼に関する材料とその数量をmaterialテーブルで管理。
materialテーブルのrecipe_idにそのカツ丼のid 1をレシピの登録画面から入力できるようにしたい。
【流れ】
入力画面でレシピを登録->session変数に保存してrecords.phpへ
問題がなければ登録ボタンで登録->データベースへreciepテーブル materialテーブルへINSERT
一覧ページでそれぞれのレシピが閲覧でき詳細を押すとreciepテーブルとmaterialテーブルのデータが組み込まれた
上の図のように閲覧可
【できてないこと】
materialテーブルのrecipe_idにreciepテーブルのidへの紐付けができてないため
reciepテーブルとmaterialテーブルが関連付けられずバラバラになってしまう。
(recipe_idにreciepテーブルのidへ制約キーをつけてます。)
reciepテーブルのid と materialテーブル recipe_id をイコール関係にしたく
リレーションなどをつけたりしたのですができませんでした。
PHP
1input_do.php 2formタグが2つあります。一つはモーダルでリンクをクリックしたら出てくるようになってます。 3 4<?php 5session_start(); 6require('dbconnect.php'); 7 8if(!empty($_POST)) { 9 10 $fileName = $_FILES['image']['name']; 11 12 if (!empty($fileName)) { 13 $ext = substr($fileName, -3); 14 if ($ext != 'jpg' && $ext != 'png' && $ext != 'gif') { 15 $error['image'] = 'type'; 16 } 17 } 18 19 20 if(empty($error)) { 21 $image = date('YmdHis') . $_FILES['image']['name']; 22 move_uploaded_file($_FILES['image']['tmp_name'],'reciep_picture/' . $image); 23 $_SESSION['recipe'] = $_POST; 24 $_SESSION['recipe']['image'] = $image; 25 26 header('Location: records.php'); 27 exit(); 28 } 29 30} 31 32 33?> 34 35<!DOCTYPE html> 36以下省略 37 <label for="method" required="required">食材</label><br> 38 <div class="ingredients" id="ingredients"> 39 <img src="image/ingredients.gif" alt="材料の画像" class="ingredients_img"> 40 </div> 41 <div class="modal"> 42 <div class="register_container"> 43 <div class="register_form"> 44 <form action="" method="post" class="form"> 45 <div class="list_header" id="list_header"> 46 <span class="name_text">名前</span> 47 <span class="quantity_text">数量</span> 48 </div> 49 <div class="row_container"> 50 <div class="row"> 51 <span class="name"> 52 <input type="text" placeholder="例: 豚肉" class="input_el" style="height: 30px;" name="name[0]" required="required"> 53 </span> 54 <span class="quantity"> 55 <input type="text" placeholder="例: 250g" class="input_el quantity_box" style="height: 30px;" name="quantity[0]" required="required"> 56 </span> 57 <a href="#" id="delete_btn" class="delete_btn"><img src="image/delete_icon.svg" alt="削除" class="delete"></a> 58 </div> 59 <div class="row"> 60 <span class="name"> 61 <input type="text" placeholder="例: 豚肉" class="input_el" style="height: 30px;" name="name[0]" required="required"> 62 </span> 63 <span class="quantity"> 64 <input type="text" placeholder="例: 250g" class="input_el quantity_box" style="height: 30px;" name="quantity[0]" required="required"> 65 </span> 66 <a href="#" id="delete_btn" class="delete_btn"><img src="image/delete_icon.svg" alt="削除" class="delete"></a> 67 </div> 68 <input type="button" value="行を追加する" class="modal_btn" id="add_btn"><hr> 69 70 <div> 71 <input type="submit" value="保存して閉じる" class="modal_btn" id="register_btn"> 72 </div> 73 </form> 74 </div> 75 </div> 76 </div> 77 <label for="method" required="required">レシピ</label><br> 78 <textarea name="text" class="register__text" cols="50" rows="10" required = required><?php print(htmlspecialchars($_SESSION['recipe']['text'],ENT_QUOTES)); ?></textarea><br> 79 <button type="submit" class="btn">確認する</button> 80 </div> 81 </form> 82 </div> 83 </div> 84 </main> 85 86</body> 87</html>
PHP
1records.php 2<?php 3session_start(); 4require('dbconnect.php'); 5 6 7 if(!empty($_POST)) { 8 9 $statment = $db->prepare('INSERT INTO reciep SET title=?,picture=?,difficulty=?,time=?,method=?,valuation=?,created=NOW()'); 10 echo $statment->execute(array( 11 $_SESSION['recipe']['title'], 12 $_SESSION['recipe']['image'], 13 $_SESSION['recipe']['difficulty'], 14 $_SESSION['recipe']['time'], 15 $_SESSION['recipe']['text'], 16 $_SESSION['recipe']['satisfy'])); 17 18 unset($_SESSION['recipe']); 19 20 header('Location: index.php'); 21 exit(); 22 } 23?> 24<!DOCTYPE html> 25<html lang="ja"> 26<head> 27 <meta charset="UTF-8"> 28 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 29 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 30 <title>COOKING RECIPE</title> 31 <link href="https://fonts.googleapis.com/css?family=Montserrat&display=swap" rel="stylesheet"> 32 <link rel="stylesheet" href="https://unpkg.com/ress/dist/ress.min.css"> 33 <link rel="stylesheet" href="css/style.css"> 34 <link href="https://use.fontawesome.com/releases/v5.6.1/css/all.css" rel="stylesheet"> 35</head> 36<body> 37 <div class="header"> 38 <div class="header__main"> 39 <h1 class="header__title u-margin-bottom-small">cooking recipe</h1> 40 <div class="header__login"> 41 <a href="login.php" class="header__login__link"> 42 <img src="image/login_icon.png" alt="ログイン" class="login__icon"> 43 <span class="login__icon-sub">ログイン</span> 44 </a> 45 </div> 46 </div> 47 </div> 48 <main> 49 <div class="section-register"> 50 <form class="register__page" method="post" action=""> 51 <input type="hidden" name="action" value="submit" /> 52 <?php if($_SESSION['recipe']['image'] !== ''): ?> 53 <img src="reciep_picture/<?php print(htmlspecialchars($_SESSION['recipe']['image'], ENT_QUOTES)); ?>" class="register__img"> 54 <?php endif; ?> 55 <h2 style="display:inline-block;"><?php print(htmlspecialchars($_SESSION['recipe']['title'],ENT_QUOTES)); ?></h2> 56 <span class="register__level"><?php print($_SESSION['recipe']['difficulty']); ?></span> 57 <span class="register__time"><?php print($_SESSION['recipe']['time']); ?>分</span> 58 <p class="paragraph"><?php print(mb_substr(htmlspecialchars($_SESSION['recipe']['text'],ENT_QUOTES),0,150)); ?></p> 59 60 <h2>材料</h2> 61 <?php 62 $count = count($_SESSION['recipe']['name']); 63 for($i=0;$i<=$count-1;$i++): 64 $stm = $db->prepare('INSERT INTO material SET recipe_id=?,name=?,quantity=?'); 65 $stm->execute(array( 66 $id, 67 $_SESSION['recipe']['name'][$i], 68 $_SESSION['recipe']['quantity'][$i])); 69 ?> 70 <p style="display:inline-block;"><?php print(htmlspecialchars($_SESSION['recipe']['name'][$i],ENT_QUOTES)); ?></p> 71 <p style="display:inline-block;"><?php print(htmlspecialchars($_SESSION['recipe']['quantity'][$i],ENT_QUOTES)); ?></p><br> 72 <?php endfor; ?> 73 <div><input type="submit" class="btn" value="登録する"></div> 74 <div><a href="input_do.php?action=rewrite" class="btn__inline">書き直す</a></div> 75 </form> 76 </div> 77 </main> 78</body> 79</html> 80
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/03/11 12:31
2020/03/13 01:15