關於課程
favoriteinsert.php
<?php
session_start();
if (isset($_SESSION[‘customer’])) {
$pdo=new PDO(‘mysql:host=localhost;dbname=shop;charset=utf8’,
‘root’, ‘12345678’);
$sql=$pdo->prepare(‘insert into favorite values(?,?)’);
$sql->execute([$_SESSION[‘customer’][‘id’], $_REQUEST[‘id’]]);
echo ‘商品加入我的最愛成功。’;
echo ‘<hr>’;
require ‘favorite.php’;
} else {
echo ‘請先登入,才能將商品加入我的最愛。’;
}
?>
favorite.php
<?php
if (isset($_SESSION[‘customer’])) {
echo ‘<table>’;
echo ‘<th>商品編號</th><th>商品名稱</th><th>價格</th>’;
$pdo=new PDO(‘mysql:host=localhost;dbname=shop;charset=utf8’,
‘root’, ‘12345678’);
$sql=$pdo->prepare(
‘select * from favorite, product ‘.
‘where customer_id=? and product_id=id’);
$sql->execute([$_SESSION[‘customer’][‘id’]]);
foreach ($sql->fetchAll() as $row) {
$id=$row[‘id’];
echo ‘<tr>’;
echo ‘<td>’, $id, ‘</td>’;
echo ‘<td><a href=”detail.php?id=’.$id.'”>’, $row[‘name’],
‘</a></td>’;
echo ‘<td>’, $row[‘price’], ‘</td>’;
echo ‘<td><a href=”favoritedelete.php?id=’, $id,
‘”>刪除</a></td>’;
echo ‘</tr>’;
}
echo ‘</table>’;
} else {
echo ‘請先登入,才能顯示我的最愛。’;
}
?>
favoritedelete.php
<?php
session_start();
if (isset($_SESSION[‘customer’])) {
$pdo=new PDO(‘mysql:host=localhost;dbname=shop;charset=utf8’,
‘root’, ‘12345678’);
$sql=$pdo->prepare(
‘delete from favorite where customer_id=? and product_id=?’);
$sql->execute([$_SESSION[‘customer’][‘id’], $_REQUEST[‘id’]]);
echo ‘所選商品已從我的最愛移除。’;
echo ‘<hr>’;
} else {
echo ‘請先登入,才能從我的最愛移除商品。’;
}
require ‘favorite.php’;
?>
favorite.show.php
<?php
session_start();
require ‘favorite.php’;
?>