replace id by name

3

I have a table called sale and in this table I insert some data from another table until everything blz, my problem and at the time of showing this data when I do an echo on the screen does not appear the name of the product but yes his id and I need the name to appear, this and my code:

<?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">';
echo '<a href="vendas.php" style="color: #ffffff" class="btn btn-inverse">Cadastrar Vendas</a></br></br>';

 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,$produtos,$servicos)=mysql_fetch_row($perfil);


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

<div id="camp1">

<label for="nome" style="color: #000"><strong>DATA :</strong> </label>
<input type="text" name="id" readonly="true" value="<?php echo $dados[2]; ?>">
<label for="nome" style="color: #000"><strong>PLACA :</strong> </label>
    <input type="text" name="id" readonly="true" 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" value="<?php echo $dados[4]; ?>">
<label for="nome" style="color: #000"><strong>PRODUTO :</strong> </label>
<input type="text" name="id" readonly="true" value="<?php echo $dados[5]; ?>">
</div>

<div id="camp3">
<label for="nome" style="color: #000"><strong>SERVIÇO :</strong> </label>
<input type="text" name="id" readonly="true" value="<?php echo $dados[6]; ?>">
</div>

I used the following code to do the id transformation for the name, but since I'm not listing the items does not work:

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

Where Product and Service I would like to see the name and not the id:

    
asked by anonymous 16.12.2015 / 13:18

2 answers

3

You could join the table of products to get the information or use the idea of the code below to do the conversion on the screen itself:

 $produtos = "SELECT * FROM produtos WHERE id=". $dados[5];
    $query = mysql_query($produtos);
    $b=mysql_fetch_array($query);
    #$id = $b ['id'];
    $produtos = $b ['produtos'];
    
16.12.2015 / 13:29
0

When you make a query and two or more tables have related information, make a join to get all the information, not multiple queries. An important detail, if the fields have equal names use an alias to prevent an array key from showing an incorrect value.

    $id = isset($_GET["id"]) && ctype_digit($_GET['id']);
    $sql = "SELECT * FROM venda as v
                     INNER JOIN produtos as p ON v.id_produto = p.id_produto
            WHERE v.id_venda='$id'";
    $sql = mysql_query($sql);
    $registro = mysql_fetch_assoc($sql);

To display the values in the form do this way, you do not need to use list() or other variables.

<input type="text" readonly="true" name="id" value="<?php echo $registro['id']; ?>">
    
16.12.2015 / 13:50