How to concatenate columns from different tables?

Asked

Viewed 264 times

1

Hello I’m with this code and I’m trying to make an adaptation to use it, but I’m having trouble bringing the contents of the column edit quant_img table, but I’m not sure how. Friends could shed some light on how I can do!!!!

Below I relate the code:

    <?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 friends.

  • Sometimes less is more. This whole code only gets in the way, it’s hard to understand what you want to do. Exactly where is the problem?

2 answers

1

From what I understand you want to take the result of the first query to display on Monday, correct?

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

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

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

0

Problems

  • You have tags meta charset in the middle of your code, tags meta should stay in the head page, and you can only specify once the charset, because it means that the document follows that Unicode pattern.
  • $total_registros not being used right below you repeat mysql_num_rows($sql_listar);
  • $conteudotabela did not start you this letting PHP set the initial value (thank goodness it is weak typing right?)
  • His last table has no end </table>.
  • I didn’t find out who it belongs to <?php } ?> after the table without end.

About quant_img

You must have some foreignKey to relate quant_img with produto, so you can realize a single Query to know the necessary data, if it is a relationship 1:1.
If it is a 1:N it is best to perform the query of quant_img inside your noose while adding WHERE codigo = {$id};, something like that, and then do one more while for the N images.

Browser other questions tagged

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