前提・実現したいこと
こんにちは。
プログラミング初心者で現在練習のためにMySQLを用いてPHPでECサイトを作成しております。
発生している問題・エラーメッセージ
SESSIONを用いてお客様がカートに入れた商品の情報を保持したいのですが、応用的なSESSIONの知識が乏しくさまざまなサイトやサンプルコードを試してもうまくいきません。
該当のソースコード
php
1//index.php 2 3 4 5<?php 6 7$host = '******'; 8$dbname = '******'; 9$dbuser = '******'; 10$dbpassword = '******'; 11 12 13try{ 14 $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8","$dbuser","$dbpassword"); 15 echo ""; 16}catch(PDOException $e){ 17 var_dump($e -> getMessage()); 18 die(); 19 20} 21 22try{ 23 $sql = "SELECT * FROM EC"; 24 $stmh = $pdo->prepare($sql); 25 $stmh->execute(); 26}catch(PDOException $Exception){ 27 die('接続エラー:' .$Exception->getMessage()); 28} 29 30 31 32 33?> 34<!DOCTYPE html> 35 36<html lang="ja"> 37 <head> 38 <meta charset="utf-8"> 39 <title>€oelacan</title> 40 <link rel="stylesheet" type="text/css" href="style.css"> 41 <link href="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/css/lightbox.css" rel="stylesheet"> 42 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script> 43 <script src="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/js/lightbox.min.js" type="text/javascript"></script> 44 </head> 45 <body> 46 <div> 47 <a href="cart.php"> 48 <img src="img/cart.png" class="cart-icon"> 49 </a> 50 </div> 51 <div> 52 <a href="index.php"> 53 <img src="img/*****.JPG" class="logo-icon"> 54 </a> 55 </div> 56 <?php 57 while($row = $stmh->fetch(PDO::FETCH_ASSOC)){ 58 ?> 59 <div class="item-list"> 60 <a href="<?=htmlspecialchars($row['image'])?>" data-lightbox="group"> 61 <img src="<?=htmlspecialchars($row['image'])?>" class="item-image"> 62 </a> 63 64 <h4> 65 <a href="detail.php?name=<?=htmlspecialchars($row['name'])?>"> 66 <?=htmlspecialchars($row['name'])?> 67 </a> 68 </h4> 69 <p><?=htmlspecialchars($row['description'])?></p> 70 <p>¥<?=htmlspecialchars($row['price'])?>-</p> 71 </div> 72 73 74 75 <?php 76 } 77 $pdo = null; 78 ?> 79 80 81 82 83 </body> 84 85</html> 86
php
1//detail.php 2 3 4<?php 5$host = '*****'; 6$dbname = '*****'; 7$dbuser = '*****'; 8$dbpassword = '*****'; 9 10 11try{ 12 $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8","$dbuser","$dbpassword"); 13 echo ""; 14}catch(PDOException $e){ 15 var_dump($e -> getMessage()); 16 die(); 17 18} 19$itemName = $_GET['name']; 20 21 22try{ 23 $sql = "SELECT * FROM EC where name = '{$itemName}'"; 24 $stmh = $pdo->prepare($sql); 25 $stmh->execute(); 26}catch(PDOException $Exception){ 27 die('接続エラー:' .$Exception->getMessage()); 28} 29 30 31 32?> 33 34<!DOCTYPE html> 35<html lang="ja"> 36 <head> 37 <meta charset="utf-8"> 38 <title>€oelacan</title> 39 <link rel="stylesheet" type="text/css" href="style.css"> 40 <link href="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/css/lightbox.css" rel="stylesheet"> 41 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script> 42 <script src="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/js/lightbox.min.js" type="text/javascript"></script> 43 </head> 44 45 46 <body> 47 <?php 48 $row = $stmh->fetch(PDO::FETCH_ASSOC); 49 50 ?> 51 <div> 52 <a href="cart.php"> 53 <img src="img/cart.png" class="cart-icon"> 54 </a> 55 </div> 56 <div> 57 <a href="index.php"> 58 <img src="img/*****.JPG" class="logo-icon"> 59 </a> 60 </div> 61 <div class="detail-item-list"> 62 <a href="<?=htmlspecialchars($row['image'])?>" data-lightbox="group"> 63 <img src="<?=htmlspecialchars($row['image'])?>" class="detail-image"> 64 </a> 65 66 <h4> 67 <?= $itemName ?> 68 </h4> 69 <p><?=htmlspecialchars($row['description'])?></p> 70 <p>¥<?=htmlspecialchars($row['price'])?>-</p> 71 <form action="cart.php?name=<?=htmlspecialchars($row['name'])?>" method="post"> 72 <select name="item-count"> 73 <?php for ($i=1; $i<=10; $i++) : ?> 74 <option class="item-count" value="<?php echo $i ?>"><?php echo $i ?></option> 75 <?php endfor; ?> 76 </select> 77 <select name="item-size"> 78 <option>S</option> 79 <option>M</option> 80 <option>L</option> 81 <option>XL</option> 82 83 </select> 84 <input type="submit" name="submit" value="Add to Cart"> 85 </form> 86 <form action="index.php" method="post"> 87 <input type="submit" name="back" value="back"> 88 </form> 89 </div> 90 </body> 91</html> 92
php
1//cart.php 2<?php 3$host = '*****'; 4$dbname = '*****'; 5$dbuser = '*****'; 6$dbpassword = '*****'; 7 8 9try{ 10 $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8","$dbuser","$dbpassword"); 11 echo ""; 12}catch(PDOException $e){ 13 var_dump($e -> getMessage()); 14 die(); 15 16} 17$itemName = $_GET['name']; 18 19 20 21 22try{ 23 $sql = "SELECT * FROM EC where name = '{$itemName}'"; 24 $stmh = $pdo->prepare($sql); 25 $stmh->execute(); 26}catch(PDOException $Exception){ 27 die('接続エラー:' .$Exception->getMessage()); 28} 29 30// $itemCount = $_POST['item-count']; 31 32$row = $stmh->fetch(PDO::FETCH_ASSOC); 33 34 35$itemCount = $_POST['item-count']; 36$itemSize = $_POST['item-size']; 37$itemSubtotal = $itemCount * htmlspecialchars($row['price']); 38$itemTotal += $itemSubtotal; 39 40 41 42 43?> 44 45<!DOCTYPE html> 46<html lang="ja"> 47 <head> 48 <meta charset="utf-8"> 49 <title>€oelacan</title> 50 <link rel="stylesheet" type="text/css" href="style.css"> 51 </head> 52 <body> 53 <div> 54 <a href="cart.php"> 55 <img src="img/cart.png" class="cart-icon"> 56 </a> 57 </div> 58 <div> 59 <a href="index.php"> 60 <img src="img/*****.JPG" class="logo-icon"> 61 </a> 62 </div> 63 64 <div class="cart-item-list"> 65 <table class="cart-subtotal"><tbody> 66 <tr><th>Item</th><th>Price</th><th>Size</th><th>Quantity</th><th>Subtotal</th></tr> 67 68 <tr> 69 <td> 70 <div class="table-image"> 71 <!-- <form class="" action="comfirm.php" method="post"> 72 <input type="hidden" name="post-image" value=""> 73 </form> --> 74 <img src="<?=htmlspecialchars($row['image'])?>" class="cart-image"> 75 </div> 76 <div class="table-name" name="cart-item-image"> 77 <a href="detail.php?name=<?=htmlspecialchars($row['name'])?>"> 78 <?php 79 echo htmlspecialchars($row['name']); 80 81 ?> 82 </a> 83 </div> 84 <div class="table-description"> 85 <?=htmlspecialchars($row['description'])?> 86 </div> 87 </td> 88 <td>¥<?=htmlspecialchars($row['price'])?>-</td> 89 <td><?= $itemSize ?></td> 90 <td> 91 <div class="table-count"> 92 <?= $itemCount ?> 93 </div> 94 </td> 95 <td> 96 <div class="table-subtotal"> 97 ¥<?= $itemSubtotal ?>- 98 </div> 99 </td> 100 101 </tr> 102 </tbody></table> 103 104 <div class="cart-total"> 105 <h3>Total</h3> 106 <p> 107 ¥<?= $itemTotal ?>- 108 </p> 109 110 </div> 111 <div class="checkout"> 112 <form action="checkout.php?name=<?=htmlspecialchars($row['name'])?>" method="post"> 113 <input type="submit" name="checkout" value="Checkout"> 114 <input type="hidden" name="item-size" value="<?= $itemSize ?>"> 115 <input type="hidden" name="item-count" value="<?= $itemCount ?>"> 116 117 </form> 118 119 </div> 120 121 </div> 122 </body> 123</html>
このページが完成しないと次の機能追加ができないためかなり困っています。
様々なコードを継ぎ足して作成しておりますので見苦しいかもしれませんが、アドバイスいただけますと幸いです。
###補足
cart.phpの後にお客様の個人情報入力ページ、最終確認ページ、完了ページを作成中です。
回答3件
あなたの回答
tips
プレビュー