php 收藏功能

<?php
/**
 * Created by PhpStorm.
 * User: liuan
 * Date: 2018-11-23 0023
 * Time: 16:25
 */
header('content-type:text/html;charset=utf-8');
include_once './lib/fun.php';
if (!$login = checkLogin()) {
    msg(2, '请登录',"login.php","collection.php");
}
$userId = (int)$_SESSION['user']['id'];
$goodsId = (int)$_GET['id'];
$type = (int)$_GET['type'];
// 数据库操作
$con = mysqlinit();
if ($type == 1) {
    //点击收藏
    $sql = "INSERT INTO `la_collection`( `user_id`, `good_id`) VALUES ({$userId},{$goodsId})";
    //商品本身表格的收藏+1
    $sql2 = "UPDATE `la_goods` set `collection_count`=`collection_count`+1 where `id`={$goodsId} and (`status`=1 or `status`=3)";


} elseif ($type == 2) {
    $sql = "DELETE FROM `la_collection` WHERE `user_id`=$userId and `good_id`=$goodsId";
    //商品本身表格的收藏-1
    $sql2 = "UPDATE `la_goods` set `collection_count`=`collection_count`-1 where `id`={$goodsId} and (`status`=1 or `status`=3) and `collection_count` > 0";
}


if (!$res = mysqli_query($con, $sql)) {
    echo "查询语句失败", mysqli_error($con), $sql;
    exit;
}

if (!$res2 = mysqli_query($con, $sql2)) {
    echo "查询语句失败", mysqli_error($con), $sql2;
    exit;
}
if (mysqli_affected_rows($con) > 0) {
    echo "<script>;history.back();</script>";

} else {
    echo "收藏或者取消收藏失败";
}



猜你喜欢

转载自blog.csdn.net/mp624183768/article/details/125880895