Ir para conteúdo
Fórum Script Brasil
  • 0

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Bar


lbfrb

Pergunta

estamos tentando mostrar o carrinho, com o produto selecionado

mas aparece o seguinte erro:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Barbie - Sereia Luzes Arco-Ãris, 1, 99.99, 99.99)' at line 1' in C:\wamp\www\fab-certo-30-11\carrinho.php on line 44

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Barbie - Sereia Luzes Arco-Ãris, 1, 99.99, 99.99)' at line 1 in C:\wamp\www\fab-certo-30-11\carrinho.php on line 44
Call Stack
# Time Memory Function Location
1 0.0000 265552 {main}( ) ..\carrinho.php:0
2 0.0156 281512 execute ( )

..\carrinho.php:44

o código:

<?php
    
    session_start();    
    if (isset($_SESSION["email"]))        
    {
        $sessao = session_id();    
    }
     else
    {
        $msg = "Não há usuario logado";
        //header("Location:carrinho_problemas.php");
    }
    
    include ("conexao.php");
    
    //se é é enviado um ID pela URL, o produto deverá ser incluido no carrinho
    if (isset($_GET["id"]))
    {    
        $idprodutos = $_GET["id"];
        
        $sql = "select * from produtos where idprodutos = $idprodutos";
        $result= $conexao->prepare($sql);
        $result->execute();
        
        if ( $linha = $result->fetch(PDO::FETCH_ASSOC) )
        {
            $nome = $linha["nome"];
            $preço = $linha["preço"];
            
            $quant = 1;
            $total = $quant * $preço;
            
            $sql = "insert into itens values(null, $idprodutos, $sessao, $nome, $quant, $preço, $total)";
                    
        $result= $conexao->prepare($sql);
        /*$result->bindValue(":idprodutos", $idprodutos);
        $result->bindValue(":sessao", $sessao);
        //$result->bindValue(":idvendas", $idvendas);
        $result->bindValue(":nome", $nome);
        $result->bindValue(":quant", $quant);
        $result->bindValue(":preço", $preço);
        $result->bindValue(":total", $total)
        */
        $result->execute();
        }
    }
    
    if (isset($_POST["atualizar"]))    
    {
        $quant = $_POST['quant'];
        // Se for diferente de vazio verificamos se é numérico
        if (is_array($quant))
        {
            // Aqui percorremos o nosso array
            foreach($quant as $iditens => $quant)
            {
                // Verificamos se os valores são do tipo numeric
                if(is_numeric($iditens) && is_numeric($quant))
                {
                    $sql = "update itens
                            set quant = $quant, total = $quant * preço
                            where iditens = $iditens";
                    $result= $conexao->prepare($sql);
                    $result->execute();
                }
            }
        }
    }

    if (isset($_GET["excluir"]))    
    {
        $excluir = $_GET['excluir'];

        $sql = "delete from itens
                where iditens = $excluir";
                
        $result= $conexao->prepare($sql);
        $result->execute();
    }

    
    $sql1    = "select * from itens where idsessao = '$sessao'";
        $result1= $conexao->prepare($sql1);
        $result1->execute();
    
    $sql2    = "select sum(total) as total from itens where idsessao = '$sessao'";
    
    $result2= $conexao->prepare($sql2);
    $result2->execute();
    
    $linha2  = mysql_fetch_array($result2);
    $total   = number_format($linha2["total"],2,",",".");
    $_SESSION["total"] = $linha2["total"];
    
    
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css/links.css" rel="stylesheet" type="text/css" />
</head>

<body>

<h1>Carrinho de Compras</h1>
<form id="form1" name="form1" method="post" action="carrinho.php">
  <table border="1" cellspacing="0" cellpadding="5">
    <tr>
      <th scope="col">Produto</th>
      <th scope="col">Quantidade</th>
      <th align="right" scope="col">Preço</th>
      <th align="right" scope="col">Total</th>
    </tr>
    <?php while ($linha = mysql_fetch_array($result1)) {?>
    <tr>
      <td><?php echo $linha["Descricao"] ?> (<a href="carrinho.php?excluir=<?php echo $linha["CodCarrinho"] ?>">excluir</a>)</td>
      <td align="center"><input id="qtde" type="text" name="qtde[<?php echo $linha["CodCarrinho"] ?>]" value="<?php echo $linha["Quantidade"] ?>" size="10" /></td>
      <td align="right"><?php echo number_format($linha["preço"],2,",",".") ?></td>
      <td align="right"><?php echo number_format($linha["Total"],2,",",".") ?></td>
    </tr>
    <?php } ?>
    <tr>
      <td colspan="4" align="right" class="detaque_em_vermelho">Valor Total: R$ <?php echo $total ?></td>
    </tr>
    
  </table>
  <p>
    <input type="submit" name="atualizar" id="atualizar" value="Atualizar Valores" />
  <a href="finalizar_compra1.php">Finalizar a Compra</a></p>
</form>
<?php
if ($linha2["Total"] == 0)
    {
        echo"<script language=javascript>
                alert('O carrinho de compras está vazio, estamos redirecionando voce para escolher um produto !');
                location.href = 'produtos_em_tabela.php';                
                //history.go(-1);
            </script>";
    }
?>
</body>
</html>


FIM

o código:

<?php
    
    session_start();    
    if (isset($_SESSION["email"]))        
    {
        $sessao = session_id();    
    }
     else
    {
        $msg = "Não há usuario logado";
        //header("Location:carrinho_problemas.php");
    }
    
    include ("conexao.php");
    
    //se é é enviado um ID pela URL, o produto deverá ser incluido no carrinho
    if (isset($_GET["id"]))
    {    
        $idprodutos = $_GET["id"];
        
        $sql = "select * from produtos where idprodutos = $idprodutos";
        $result= $conexao->prepare($sql);
        $result->execute();
        
        if ( $linha = $result->fetch(PDO::FETCH_ASSOC) )
        {
            $nome = $linha["nome"];
            $preço = $linha["preço"];
            
            $quant = 1;
            $total = $quant * $preço;
            
            $sql = "insert into itens values(null, $idprodutos, $sessao, $nome, $quant, $preço, $total)";
                    
        $result= $conexao->prepare($sql);
        /*$result->bindValue(":idprodutos", $idprodutos);
        $result->bindValue(":sessao", $sessao);
        //$result->bindValue(":idvendas", $idvendas);
        $result->bindValue(":nome", $nome);
        $result->bindValue(":quant", $quant);
        $result->bindValue(":preço", $preço);
        $result->bindValue(":total", $total)
        */
        $result->execute();
        }
    }
    
    if (isset($_POST["atualizar"]))    
    {
        $quant = $_POST['quant'];
        // Se for diferente de vazio verificamos se é numérico
        if (is_array($quant))
        {
            // Aqui percorremos o nosso array
            foreach($quant as $iditens => $quant)
            {
                // Verificamos se os valores são do tipo numeric
                if(is_numeric($iditens) && is_numeric($quant))
                {
                    $sql = "update itens
                            set quant = $quant, total = $quant * preço
                            where iditens = $iditens";
                    $result= $conexao->prepare($sql);
                    $result->execute();
                }
            }
        }
    }

    if (isset($_GET["excluir"]))    
    {
        $excluir = $_GET['excluir'];

        $sql = "delete from itens
                where iditens = $excluir";
                
        $result= $conexao->prepare($sql);
        $result->execute();
    }

    
    $sql1    = "select * from itens where idsessao = '$sessao'";
        $result1= $conexao->prepare($sql1);
        $result1->execute();
    
    $sql2    = "select sum(total) as total from itens where idsessao = '$sessao'";
    
    $result2= $conexao->prepare($sql2);
    $result2->execute();
    
    $linha2  = mysql_fetch_array($result2);
    $total   = number_format($linha2["total"],2,",",".");
    $_SESSION["total"] = $linha2["total"];
    
    
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css/links.css" rel="stylesheet" type="text/css" />
</head>

<body>

<h1>Carrinho de Compras</h1>
<form id="form1" name="form1" method="post" action="carrinho.php">
  <table border="1" cellspacing="0" cellpadding="5">
    <tr>
      <th scope="col">Produto</th>
      <th scope="col">Quantidade</th>
      <th align="right" scope="col">Preço</th>
      <th align="right" scope="col">Total</th>
    </tr>
    <?php while ($linha = mysql_fetch_array($result1)) {?>
    <tr>
      <td><?php echo $linha["Descricao"] ?> (<a href="carrinho.php?excluir=<?php echo $linha["CodCarrinho"] ?>">excluir</a>)</td>
      <td align="center"><input id="qtde" type="text" name="qtde[<?php echo $linha["CodCarrinho"] ?>]" value="<?php echo $linha["Quantidade"] ?>" size="10" /></td>
      <td align="right"><?php echo number_format($linha["preço"],2,",",".") ?></td>
      <td align="right"><?php echo number_format($linha["Total"],2,",",".") ?></td>
    </tr>
    <?php } ?>
    <tr>
      <td colspan="4" align="right" class="detaque_em_vermelho">Valor Total: R$ <?php echo $total ?></td>
    </tr>
    
  </table>
  <p>
    <input type="submit" name="atualizar" id="atualizar" value="Atualizar Valores" />
  <a href="finalizar_compra1.php">Finalizar a Compra</a></p>
</form>
<?php
if ($linha2["Total"] == 0)
    {
        echo"<script language=javascript>
                alert('O carrinho de compras está vazio, estamos redirecionando voce para escolher um produto !');
                location.href = 'produtos_em_tabela.php';                
                //history.go(-1);
            </script>";
    }
?>
</body>
</html>


FIM

Link para o comentário
Compartilhar em outros sites

0 respostass a esta questão

Posts Recomendados

Até agora não há respostas para essa pergunta

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      651,8k
×
×
  • Criar Novo...