replace id by name

Asked

Viewed 1,099 times

3

Good guys I have a table called sale and in this table I insert some data from other table up to there all blz , my problem and at the time of showing this data when I echo a screen does not appear the name of the product but rather the id of it and I need to appear the name , 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 , more like 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 are you Product and Service that I would like the name to appear and not the id :

mostra

  • First you have to make sure that in the sales table there are the fields with the service and product names. Otherwise you should make a Join in the tables that have these names. A good practice is also to always bring only the fields you will use instead of using the *.

2 answers

3


You could make a Join in the product table to get the information or use the idea of the code below to make 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'];
  • I made some small editions and gave straight , thank you bro ;)

0

When making a query and two or more tables have related information do a Join to get the information of all and not multiple queries. An important detail, if the fields have equal names use an alias to prevent an array key from displaying 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 so, you do not need to use list() or other variables.

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

Browser other questions tagged

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