Add Product to Shopping Cart - PHP

Asked

Viewed 168 times

1

Hello, I’m trying to make a virtual store in PHP, but whenever I add a product to the shopping cart the program can not access the database and take the product code to display.

<?php
                include("class/conexao.class.php");
                if(count($_SESSION['carrinho']) == 0){
                    echo '<tr><td colspan="5">Não há produto no carrinho</td></tr>';
                }
                else{
                    $conexao = new Conexao();
                    $total = 0;
                    foreach($_SESSION['carrinho'] as $cod => $qtd){
                        $sql    = "SELECT * FROM rec_Produtos WHERE cod='$cod'";
                        $status = sqlsrv_query($conexao, $sql);
                        while ($dados = sqlsrv_fetch_array($status, SQLSRV_FETCH_NUMERIC)){
                            $nome   = $dados[1];
                            $preco = $dados[3];
                            $sub = number_format($preco*$qtd, 2, ',', '.');
                        }
                        $total += $sub;
                        echo '<tr>
                                <td>'.$nome.'</td>
                                <td><input type="text" size="3" name="prod['.$cod.']" value="'.$qtd.'" /></td>
                                <td>R$ '.$preco.'</td>
                                <td>R$ '.$sub.'</td>
                                <td><a href="?acao=del&id='.$cod.'">Remover</a></td>
                            </tr>';
                    }
                    $_SESSION['total'] = number_format($total, 2, ',', '.');
                    echo '<tr>
                            <td colspan="4">Total</td>
                            <td><h5>R$ '.$_SESSION['total'].'</h5></td>
                        </tr>';
                }
?>

This is my Connection class

<?php
    class conexao{
        public $con;
        protected $stmt;
        protected $host = '******';
        protected $database = '******';
        protected $usuario = '******';
        protected $senha = '******';

        public function __construct(){
            $this->con = new PDO("sqlsrv:Server=".$this->host.";Database=".$this->database.";ConnectionPooling=0",$this->usuario,$this->senha);
            if($this->con === false){
                die(print_r(sqlsrv_errors(),true));
            }
            return true;
        }

        private function execSQL($consulta){
            if($consulta === ''){
                return false;
            }
            $stmt = $this->con->prepare($consulta);
            if($stmt){
                $this->stmt=$stmt;
            }
            else{
                $this->sql_error($consulta);
            }
        }

        public function exec($query, $dados) {
            $this->execSQL($query);
            $this->stmt->execute($dados);
        }

        public function select($query, $dados) {
            $this->execSQL($query);
            $this->stmt->execute($dados);
            return $this->stmt->fetchAll();
        }

        public function selectUser($login,$senha){
            $sql = "SELECT * from rec_Cliente where email= ? and senha= ?";
            $this->execSQL($sql);
            $this->stmt->execute(array($login,$senha));
            return $this->stmt->fetchAll();
            //$dados='';
            //while($linha = sqlsrv_fetch_array($this->stmt, SQLSRV_FETCH_ASSOC)){
            //    $dados[] = $linha;
            //}
            //return $dados;
        }

        private function sql_error($sql){
            echo sqlsrv_error($this->con).'<br>';
            die('error: '.$sql);
        }
    }
?>

And that’s the connection class for the products

<?php
class conexaoProdutos{
    public $con;
    protected $stmt;
    protected $host = '******';
    protected $database = '******';
    protected $user = '******';
    protected $senha = '******';

    public function __construct(){
        $connectionInfo = array("Database"=>$this->database,"PWD"=>$this->senha,"UID"=>$this->user);
        $this->con = sqlsrv_connect($this->host,$connectionInfo);
        if($this->con === false){
            die(print_r(sqlsrv_errors(),true));
        }
        return true;
    }

    public function __destruct(){
        sqlsrv_close($this->con);
    }

    private function execSQL($consulta){
        if($consulta === ''){
            return false;
        }
        $stmt = sqlsrv_query($this->con,$consulta);
        if($stmt){
            $this->stmt=$stmt;
        }
        else{
            $this->sql_error($consulta);
        }
    }

    public function getTxtDescricao(){
        $sql = "SELECT descricao from rec_Produtos";
        $this->execSQL($sql);
        $dados[]='';
        $i=0;
        while($linha = sqlsrv_fetch_array($this->stmt,SQLSRV_FETCH_NUMERIC)){
            $dados[$i] = $linha[0];
            $i++;
        }
        return $dados;
    }

    public function getProduto(){
        $sql = "SELECT * from rec_Produtos";
        $this->execSQL($sql);
        $dados[]='';
        $i=0;
        while($linha = sqlsrv_fetch_array($this->stmt,SQLSRV_FETCH_NUMERIC)){
            $dados[$i][0] = $linha[0];
            $dados[$i][1] = $linha[1];
            $dados[$i][2] = $linha[2];
            $dados[$i][3] = $linha[3];
            $i++;
        }
        return $dados;
    }

    public function getValor(){
        $sql = "SELECT valor from rec_Produtos";
        $this->execSQL($sql);
        $dados[]='';
        $i=0;
        while($linha = sqlsrv_fetch_array($this->stmt,SQLSRV_FETCH_NUMERIC)){
            $dados[$i] = $linha[0];
            $i++;
        }
        return $dados;
    }

    private function sql_error($sql){
        echo sqlsrv_error($this->con).'<br>';
        die('error: '.$sql);
    }
}
?>

É assim que fica quando eu tento adicionar um item ao carrinho

If anyone can help me I’d appreciate it.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.