List only data from the same id

Asked

Viewed 456 times

0

Good guys after a long time hitting the head with a certain problem I can solve it in a certain way (gambiarra) but now I need to know how to list data from my database that is of the same id , i add several products in a certain sale and I wanted to list these products only from that sale , I tried to use the while but it returns me a repeated list of the values of my entire table

My code :

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script>

        function alguma(e){
            if(!confirm("Deseja realmente excluir este registro?"))
                cancelOperation(event);
        }
        function cancelOperation(e){
            var evt = e || window.event;
            if(evt.preventDefault())
                evt.preventDefault();
            else
                evt.returnValue = false;
        }
    </script>
</head>

<?php
include("banco.php");
echo'<link rel="stylesheet" type="text/css" href="css/estilo.css">';
echo'<link rel="stylesheet" type="text/css" href="css/bootstrap.css">';
 include("banco.php");

   $id = $_GET["id"];

        $sql = mysql_query("select * from venda where id_venda='$id'");

        $exibe = mysql_fetch_assoc($sql);

        $perfil=mysql_query("SELECT * FROM venda WHERE id_venda='$id'");

        $dados=list($id_venda,$venda,$data,$placa,$km)=mysql_fetch_row($perfil);

$produtos = "SELECT * FROM produtos WHERE id_produto=". $dados[5];
$query = mysql_query($produtos);
$b=mysql_fetch_array($query);
#$id = $b ['id'];
$produtos = $b ['produtos'];


$servicos = "SELECT * FROM servicos WHERE id_servico=". $dados[6];
$query = mysql_query($servicos);
$c=mysql_fetch_array($query);
#$id = $b ['id'];
$servicos = $c ['servicos'];


?>
<label for="nome" style="color: #000"><strong>ID DA VENDA :</strong> </label>
<input type="text" readonly="true" name="id" style="width: 170px" value="<?php echo $dados[0]; ?>">

<div id="camp5">
<label for="nome"readonly="true" style="color: #000"><strong>Nº DA VENDA:</strong> </label>
<input type="text" name="id" readonly="true" style="width: 170px" value="<?php echo $dados[1]; ?>">
</div>

<div id="camp1">
<label for="nome" style="color: #000"><strong>DATA :</strong> </label>
<input type="text" name="id" readonly="true" style="width: 170px" value="<?php echo $dados[2]; ?>">
</div>

<div id="camp6">
<label for="nome" style="color: #000"><strong>PLACA :</strong> </label>
    <input type="text" name="id" readonly="true" style="width: 170px" value="<?php echo $dados[3]; ?>">
</div>

<div id="camp2">
<label for="nome" style="color: #000"><strong>KM :</strong> </label>
<input type="text" name="id" readonly="true" style="width: 170px" value="<?php echo $dados[4]; ?>">

</div>

<br><br>
<div id="adicionar">
    <a href="adcproduto.php" class="btn btn-info    ">ADICIONAR MAIS PRODUTOS&nbsp;&nbsp;<img src="img/add-icon.png"/></a><br><br>
</div>

<label for="nome" style="color: #000"><strong>PRODUTO :</strong> </label>
<input type="text" name="id" style="width: 450px" readonly="true"  value="<?php echo $produtos; ?>"><br>

<?php
$selbanco = "SELECT * FROM pedido";
$querybanco = mysql_query($selbanco);

$sql1 = mysql_query("select * from pedido where id_venda='$id'");

$exibe1 = mysql_fetch_assoc($sql1);

$perfil1=mysql_query("SELECT * FROM pedido WHERE id_venda='$id'");

$lista=list($id, $produtos , $idvenda)=mysql_fetch_row($perfil1);


?>

<input type="text" name="id" style="width: 450px" readonly="true"  value="<?php echo $lista[1]; ?>"><br>


</body>
</html>

In this part I make to appear the products more only appears the first , and when I tried with the while it returns me all the data of the table, someone knows how I can do it ? :

<?php
$selbanco = "SELECT * FROM pedido";
$querybanco = mysql_query($selbanco);

$sql1 = mysql_query("select * from pedido where id_venda='$id'");

$exibe1 = mysql_fetch_assoc($sql1);

$perfil1=mysql_query("SELECT * FROM pedido WHERE id_venda='$id'");

$lista=list($id, $produtos , $idvenda)=mysql_fetch_row($perfil1);


?>

<input type="text" name="id" style="width: 450px" readonly="true"  value="<?php echo $lista[1]; ?>"><br>
  • I did not understand why the same query is made twice (request)

  • So this is kind of the gambit that I did

1 answer

0


Look has 2 Sqls in the same table in your example, in my view there are two things, either you use only one table to save the sale or you must have wrong the table name in the second SQL. By way of example I would keep this type of transaction in at least two tables:

tab_vendas
tab_vendas_itens

The sales table stores specific order information (Total Value, Customer Price, Notafiscal, etc.), ja to items (Product Code, Value Added, Price Discounted, Quantity and etc)

This is an example structure for storing information, but let’s assume that it is only using one table, in the first SQL you must specify only fields that do not contain unique data such as sequential table ID and not "*", then you use DISTINCT not to bring duplicate data.

Browser other questions tagged

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