首页 > 解决方案 > 1类中的多个sql语句

问题描述

我是 OOP php 的新手,我正在尝试创建一个简单的添加到购物车功能,当用户将产品添加到购物车时,产品 ID 将被插入到购物车表中,我想从帖子中获取产品价格根据购物车表中的产品 ID 表添加到购物车价格列,产品 ID 已插入购物车表但我无法获取价格。

<?php
class Post{
    private $db;

    public function __construct(){
        $this->db = new Database;
    }

    public function getprice($id){
        $this->db->query('SELECT * FROM  posts WHERE id = :id');
        $this->db->bind(':pirce', $price);

        $results = $this->db->resultSet();
        return $results;
    }


    //add to cart
    public function addcart($data){

        $price = $this->getprice($id);

        $this->db->query('INSERT INTO cart (p_id,  size ,price) VALUE (:id, :qty, $price)');

        //bind values
        $this->db->bind(':id', $data['id']);
        $this->db->bind(':qty', $data['qty']);
        $this->db->bind($price);

        // Execute
        if($this->db->execute()){
            return true;
        } else {
            return false;
        }
    }
 }

购物车表

发布表

标签: php

解决方案


<?php
class Post{
    private $db;

    public function __construct(){
        $this->db = new Database;
    }

    public function getprice($id){
        $this->db->query('SELECT * FROM  posts WHERE id = :id');
        $this->db->bind(':id', $id);

        $results = $this->db->resultSet();
        return $results;
    }


    //add to cart
    public function addcart($data){

        $post = $this->getprice($data['id']);

        $this->db->query('INSERT INTO cart (p_id,  size ,price) VALUE (:id, :qty, :price)');

        //bind values
        $this->db->bind(':id', $data['id']);
        $this->db->bind(':qty', $data['qty']);
        $this->db->bind(':price', $post->price);

        // Execute
        if($this->db->execute()){
            return true;
        } else {
            return false;
        }
    }
 }

推荐阅读