0
I’m making a shopping cart file. The code is apparently correct, but when including the file connecting to the database, the browser displays the message "In the database".
Note: Both the file, called "Request.php", and the connection to the database (connection.php) are in the same directory.
Here is the code:
<?php
include 'conexao.php';
session_start();
if(!isset($_SESSION['carrinho'])){
$_SESSION['carrinho'] = array();
}
//ADICIONA PRODUTO
if(isset($_GET['acao'])){
//ADICIONA CARRINHO
if($_GET['acao'] == 'add'){
$id = intval($_GET['id']);
if(!isset($_SESSION['carrinho'][$id])){
$_SESSION['carrinho'][$id] = 1;
}else{
$_SESSION['carrinho'][$id] += 1;
}
}
}
?>
<table>
<caption>Carrinho de compras</caption><br>
<thead>
<tr>
<br><br>
<th width="244">Produto</th>
<th width="79">Quantidade</th>
<th width="89">Preço </th>
<th width="100">Subtotal</th>
<th width="64">Remover</th>
</tr>
</thead>
<form action="?acao=up" method="POST">
<tfoot>
<tr>
<td colspan="5"><input type="submit" value="Atualizar carrinho"/></td>
</tr>
<tr>
<td colspan="5"><a href="principal.php">Continuar comprando</a></td>
</tr>
</tfoot>
<tbody>
<?php
if(count($_SESSION['carrinho']) == 0){
echo '<tr><td colspan="5">Não há produtos no carrinho</tr>';
}
else{
foreach($_SESSION['carrinho'] as $id => $qtd){
$sql = "SELECT * FROM produto WHERE id= '$id'";
$result = mysql_query($sql) or die (mysql_error());
$produto = mysql_fetch_assoc($result);
$nome = $produto['nome'];
$preco = number_format($produto['preco'], 2, ',', '.');
$sub = number_format($produto['preco'] * $qtd, 2, ',', '.');
echo '<tr>
<td>'.$nome.'</td>
<td><inpu type="text" size ="3" name="produto['.$id.'] value="'.$qtd.'"></td>
<td>'.$preco.'</td>
<td>'.$sub.'</td>
<td><a href=?acao=del&id='.$id.'>Remover</a></td>
</tr>';
}
}
?>
</tbody>
</form>
</table>
</body>