前提・実現したいこと
PHPでECサイトのカートを作成中です。
実現したいことは
6.1.ショッピングカートにある商品の合計を表示する。
6.2.指定の商品を削除することができる(カート情報はデータベースの値を更新する)。
6.3.指定の商品を数量を変更することができる(カート情報はデータベースの値を更新する)。
6.4.商品を購入する(「購入完了ページ」に遷移する)。
6.5.カート一覧から購入予定の商品数を変更する場合、正の整数のみ可能とする(0は含まない)。正の整数以外はエラーメッセージを表示して、変更できない。
自分なりにやってみたのですがうまくいきません。どなたか教えていただけると助かります><
該当のソースコード
<?php $host = ''; $username = ''; $passwd = ''; $dbname = ''; $item_name = ''; $price =''; $amount =''; $item_id =''; $sql_kind = ''; $delete = ''; $err_msg = array(); $success_msg = array(); $data = array(); $img_dir = './img/'; session_start(); if(isset($_SESSION['user_id']) === FALSE) { header('Location: login.php'); exit; } $user_id = $_SESSION['user_id']; $user_name = $_SESSION['user_name']; $link = mysqli_connect($host, $username, $passwd, $dbname); if ($link) { mysqli_set_charset($link, 'utf8'); if($_SERVER['REQUEST_METHOD'] === 'POST'){ if(isset($_POST['sql_kind']) === TRUE) { $sql_kind = $_POST['sql_kind']; } if($sql_kind === 'new'){ if(isset($_POST['item_name']) === TRUE) { $item_name = $_POST['item_name']; } if(isset($_POST['price']) === TRUE) { $price = $_POST['price']; } if(isset($_POST['amount']) === TRUE) { $amount = $_POST['amount']; }else if ($sql_kind === 'amount') { if(isset($_POST['cart_id'])) { $cart_id = $_POST['cart_id']; } if(isset($_POST['amount'])){ $amount = $_POST['amount']; } if (mb_strlen($amount) === 0){ $err_msg[] = '個数を入力してください'; } else if (ctype_digit($amount) === FALSE) { $err_msg[] = '個数は数字で入力してください。'; } if (ctype_digit($cart_id) === FALSE) { $err_msg[] = "不正な要求です。"; } if(count($err_msg) === 0) { $sql = 'UPDATE item_cart_table SET amount = ' . $amount . ' WHERE cart_id = ' . $cart_id; if(mysqli_query($link,$sql) === TRUE){ $success_msg [] = '個数を変更しました'; }else{ $err_msg[] = 'UPDATE item_cart_table:updateエラー:' . $sql; } } } } }else if ($sql_kind === 'delete'){ if(isset($_POST['cart_id'])) { $cart_id =$_POST['cart_id']; } if (ctype_digit($cart_id) === FALSE) { $err_msg[] = "不正な要求です。"; } if(count($err_msg) === 0) { mysqli_autocommit($link,false); $sql = 'DELETE FROM item_cart_table WHERE cart_id = ' . $cart_id; if(mysqli_query($link,$sql) === TRUE){ $sql = 'DELETE FROM item_cart_table WHERE cart_id = ' . $cart_id; if(mysqli_query($link,$sql) === FALSE) { $err_msg[] = 'item_cart_table:DELETEエラー' . $sql; } }else{ $err_msg[] = '削除に失敗しました。'; } if (count($err_msg) === 0) { mysqli_commit($link); $success_msg[] = '商品削除成功'; } else { mysqli_rollback($link); } } } $sql = 'SELECT item_cart_table.*, item_info_table.* FROM item_cart_table JOIN item_info_table ON item_info_table.item_id = item_cart_table.item_id'; if ($result = mysqli_query($link, $sql)) { while ($row = mysqli_fetch_assoc($result)) { $data[] = $row; } } else { $err_msg[] = 'SQL失敗:' . $sql; } mysqli_free_result($result); mysqli_close($link); } else { $err_msg[] = 'error: ' . mysqli_connect_error(); } ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset ="UTF-8"> <title>カート</title> <style> table, td, th { border: solid black 1px; } table { width: 800px; } img { width: 150px; } </style> </head> <body> <?php foreach ($err_msg as $value) { ?> <p><?php print $value; ?></p> <?php } ?> <?php foreach ($success_msg as $value) { ?> <p><?php print $value; ?></p> <?php } ?> <section> <h2>ショッピングカート</h2> <span>ユーザ名:<?php print $user_name; ?>様</span> <a href="logout.php">ログアウト</a> <a href="item_cart.php">カート</a> <table> <tr> <th>商品画像</th> <th>商品名</th> <th>価格</th> <th>数量</th> <th>操作</th> </tr> <form action="complete.php" method="post"> <?php foreach($data as $value) { ?> <tr> <td><img src="<?php print $img_dir . $value['img']; ?>"></td> <td><?php print htmlspecialchars($value['item_name'], ENT_QUOTES,'UTF-8'); ?></td> <td><?php print $value['price']; ?></td> <form method="post"> <td> <input type="text" name="amount" value="<?php print $value['amount'];?>"> <input type="submit" value="変更"> <input type="hidden" name="sql_kind" value="amount"> <input type="hidden" name="cart_id" value="<?php print $value['cart_id']?>"> </td> </form> <form method="post"> <td> <input type="submit" value="削除"> <input type="hidden" name="sql_kind" value="delete"> <input type="hidden" name="cart_id" value="<?php print $value['cart_id']?>"> </td> </form> </form> </tr> </table> <span>合計<?php print $sum; ?></span> <div id="submit"><input type="submit" value="購入する"></div> </form> <a href="item_list_php">商品一覧に戻る</a> <?php } ?> </section> </body> </html>
回答2件
あなたの回答
tips
プレビュー