How to concatenate columns from different tables?

1

Hello, I'm having this code and I'm trying to make an adaptation to use it, but I'm having trouble bringing the contents of the edit column of the quant_img table, but I'm not sure how. Friends could give me a light of how I can do !!!!

Below is the code below:

    <?php
    include 'cabecalho.php';
    ?>
    <?php
        include '../conexao.php';

                $editar = $_POST['editar']; 
                $query = mysql_query("SELECT * FROM quant_img");
                $res = mysql_fetch_array($query);

    ?>

    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />

    <div align="center"  style="margin: 0 0 0 180px; max-width:1000px; width: 90%;">
    <div align="left" style="margin:0 0 0 10px;"><h3>Atualização de Produtos</h3></div>

    <div align="center" style=" padding:2px; width:655px; height:auto; float:left;">

        <div align="left" style=" padding:2px; width:315px; height:auto; float:left;">
    <label>Entre com o código do produto</label>
    <form action="prod_consulta.php" enctype="multipart/form-data" name="busca" method="post">
    <input size="6" type="text" value="" name="buscar"/>
        <input type="submit" name="busca" value="Buscar Produto"/>
    </form>
        </div>    
    </div>

    <div align="left" style=" padding:2px; width:1000px; height:auto; border-top:solid 2px; float:left;">

    <br />

    <?php

    $buscar = $_POST['buscar']; 
    $sql_listar = mysql_query("SELECT * FROM produto WHERE codigo LIKE '%$buscar%'");

    $total_registros = mysql_num_rows($sql_listar);

        if(mysql_num_rows($sql_listar) <= 0){
            echo '<script type="text/javascript">
                alert("Desculpe! Nenhum produto foi encontrado com esse código!");
                window.location.href = "prod_upd.php";
                </script>';
        }else{

        while($res = mysql_fetch_array($sql_listar)){
            $codigo = $res['codigo'];

            $codigo = $_POST['codigo'];
            $titulo = $_POST['titulo'];
            $descricao = $_POST['descricao'];
            $preco = $_POST['preco'];
            $codcategoria = $_POST['codcategoria'];
            $codmarca = $_POST['codmarca'];
            $img01 = $_POST['img01'];

            $conteudotabela .= '<tr style="color:#090;">

                    <td align="center">'.$res['codigo'].'</td>

                    <td align="center">'.$res['titulo'].'</td>

                    <td align="center">'.$res['preco'].'</td>

                    <td align="center">
                    <img width="100" height="auto" src="../img_produtos/'.$res['img01'].'" /></td>

                    <td align="center">

                    <a style="text-decoration:none;" href="'.$res['editar'].'?codigo='.$res['codigo'].'">
                    <img width="25" src="../img/edit.png" title="Editar Produto código '.$res['codigo'].'"/>
                    </a>

                    <a style="text-decoration:none;" href="prod_del.php?codigo='.$res['codigo'].'">
                    <img width="25" src="../img/del.png" title="Excluir Produto código '.$res['codigo'].'"/>
                    </a>
                    </td>

                </tr>';
        }


    ?>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />

    <table class="tbllista" style="width: 15%">
        <thead>
            <tr align="center">
                <th style="width: 5%"></th>
                <th style="width: 5%">
            </tr>
        </thead>
    </table>

    <table class="tbllista" style="width: 100%">
        <thead>
            <tr align="center">
                <th align="center">Código</th>
                <th align="center">Título</th>
                <th align="center">Preço</th>
                <th align="center">Imagem</th>
                <th align="center">Operações</th>
             </tr>
        </thead>
        <tbody>
                <?php echo $conteudotabela; ?>

        </tbody>

    <?php   
        }
    ?>

    </div>

Thank you for the attention of your friends.

    
asked by anonymous 24.03.2016 / 00:37

2 answers

1

So I understand you want to get the result of the first query to display in the second, correct?

I will assume that the quant_img query will return a result.

$query = mysql_query("SELECT * FROM quant_img");
// query, linha, coluna
echo mysql_result($query , 0, "editar"); 

In this echo you will get the first line of the query above, and it will show the value of the edit column

    
24.03.2016 / 02:23
0

Problems

  • You have meta charset tags in the middle of your code, meta tags must be in head of the page, and you can only specify charset once, since it means that the document follows that unicode pattern.
  • $total_registros is not being used right below you repeat mysql_num_rows($sql_listar);
  • $conteudotabela was not started you are letting PHP set the initial value (still good that it is typed weak right?)
  • Your last table does not have a </table> term.
  • I did not find, who belongs <?php } ?> soon after the table without ending.

About quant_img

You must have some foreignKey to relate quant_img to produto , so you can perform a single Query to know the required data, if it is a 1: 1 relationship. If it is a 1: N it is best to query the quant_img within your loop while by adding WHERE codigo = {$id}; , something like this, and then make another while for N images.

    
24.03.2016 / 14:10