Erro:查询是空的

时间:2016-01-18 18:38:10

标签: php mysql

每当我尝试从另一个页面获取变量时,我都会遇到此错误。

错误:

  

警告:sprintf():参数太少了   /srv/users/distacacessorios/apps/distacacessorios/public/relatorio/busca.php   在第69行   查询为空

的index.php

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="utf-8"/>
    <title>Busca de produtos</title>
</head>
<body>
    <h1>Selecione a sua lista</h1>
    <form action="busca.php" method="post">
        <input type="text" name="palavra" />
        <select name="categoria">
            <option value="ferramentas">Ferramentas</option>
            <option value="acessorios">Acessórios</option>
            <option value="todos">Todos</option>
        </select>
        <input type="submit" Value="Buscar" />
    </form>
</body>
</html>

buscaproduto.php

<?php
// definições de host, database, usuário e senha
$host = "localhost";
$db   = "xxx";
$user = "xxx";
$pass = "xxx";

$busca = $_POST['palavra'];
echo $busca;
$con = mysql_pconnect($host, $user, $pass);
mysql_select_db($db, $con);
$query = sprintf("SELECT 
    p.id as ID,
    sk.meta_value AS SKU,
    p.post_title AS PRODUTO,
    pc.meta_value AS PRECO,
    ap.meta_value AS APLICACAO,
    fb.meta_value AS FABRICANTE,
    ct.name AS CATEGORIA,
    im2.meta_value AS IMAGEM
FROM wp_posts p

INNER JOIN wp_postmeta pr ON (
    p.ID = pr.post_id AND
    p.post_type = 'product' AND
    p.post_title like '%".$busca."%'
)
INNER JOIN wp_postmeta es ON (
    p.ID = es.post_id AND
    es.meta_key = '_stock'
)
INNER JOIN wp_postmeta sk ON (
    p.ID = sk.post_id AND
    sk.meta_key = '_sku'
)
INNER JOIN wp_postmeta pc ON (
    p.ID = pc.post_id AND
    pc.meta_key = '_price'
)
INNER JOIN wp_postmeta ap ON (
    p.ID = ap.post_id AND
    ap.meta_key = 'aplicacao'
)
INNER JOIN wp_postmeta fb ON (
    p.ID = fb.post_id AND
    fb.meta_key = 'fabricante'
)
LEFT JOIN wp_postmeta im1 ON (
    im1.post_id = p.id AND
    im1.meta_value IS NOT NULL AND 
    im1.meta_key = '_thumbnail_id'              
)
LEFT JOIN wp_postmeta im2 ON (
    im1.meta_value = im2.post_id AND 
    im2.meta_key = '_wp_attached_file' AND 
    im2.meta_value IS NOT NULL  
)
INNER JOIN wp_term_relationships rl1 ON (
    p.ID = rl1.object_id
)
INNER JOIN wp_term_taxonomy rl2 ON (
    rl1.term_taxonomy_id = rl2.term_taxonomy_id
)
INNER JOIN wp_terms ct ON (
    rl2.term_id = ct.term_id AND
    ct.term_id = 795 OR
    ct.term_id = 846
)
GROUP BY p.ID");
$dados = mysql_query($query, $con) or die(mysql_error());
$linha = mysql_fetch_assoc($dados);
$total = mysql_num_rows($dados);
?>

<html>
        <head>
        <title>Relatórios</title>
        <style>
                table tr:nth-child(even) {
                        background-color: #eee;
                }
                table tr:nth-child(odd) {
                        background-color: #fff;
                }
                table th {
                        color: white;
                        border: 1px solid black;
                        background-color: black;
                }
                img {
                        height: 100px;
                        width: 100px;
                        margin: 5px;
                }
        </style>
<body>
        <div id="content_area_div">
                <h1>PRODUTOS</h1>
                <table style="width:100%; text-align:center;">
                        <tr>
                                <th>ID</th>
                                <th>SKU</th>
                                <th>PRODUTO</th>
                                <th>PREÇO</th>
                                <th>APLICAÇÃO</th>
                                <th>FABRICANTE</th>
                                <th>CATEGORIA</th>
                                <th>IMAGEM</th>
                        </tr>
        <?php
                if($total > 0)
                {
                        do
                        {
        ?>
                        <tr>
                                <td><?=$linha['ID']?></td>
                                <td><?=$linha['SKU']?></td>
                                <td><?=$linha['PRODUTO']?></td>
                                <td><?=$linha['PRECO']?></td>
                                <td><?=$linha['APLICACAO']?></td>
                                <td><?=$linha['FABRICANTE']?></td>
                                <td><?=$linha['CATEGORIA']?></td>
                                <td><img src="http://www.distacacessorios.com.br/wp-content/uploads/<?=$linha['IMAGEM']?>/"></td>
                        </tr>
                <?php
                        }
                        while($linha = mysql_fetch_assoc($dados));
                }
                ?>
                </table>
        </div>
</body>
</html>
<?php

1 个答案:

答案 0 :(得分:1)

为什么要使用sprintf()??

$query = "SELECT 
    p.id as ID,
    sk.meta_value AS SKU,
    p.post_title AS PRODUTO,
    pc.meta_value AS PRECO,
    ap.meta_value AS APLICACAO,
    fb.meta_value AS FABRICANTE,
    ct.name AS CATEGORIA,
    im2.meta_value AS IMAGEM
FROM wp_posts p

INNER JOIN wp_postmeta pr ON (
    p.ID = pr.post_id AND
    p.post_type = 'product' AND
    p.post_title like '%".$busca."%'
)
INNER JOIN wp_postmeta es ON (
    p.ID = es.post_id AND
    es.meta_key = '_stock'
)
INNER JOIN wp_postmeta sk ON (
    p.ID = sk.post_id AND
    sk.meta_key = '_sku'
)
INNER JOIN wp_postmeta pc ON (
    p.ID = pc.post_id AND
    pc.meta_key = '_price'
)
INNER JOIN wp_postmeta ap ON (
    p.ID = ap.post_id AND
    ap.meta_key = 'aplicacao'
)
INNER JOIN wp_postmeta fb ON (
    p.ID = fb.post_id AND
    fb.meta_key = 'fabricante'
)
LEFT JOIN wp_postmeta im1 ON (
    im1.post_id = p.id AND
    im1.meta_value IS NOT NULL AND 
    im1.meta_key = '_thumbnail_id'              
)
LEFT JOIN wp_postmeta im2 ON (
    im1.meta_value = im2.post_id AND 
    im2.meta_key = '_wp_attached_file' AND 
    im2.meta_value IS NOT NULL  
)
INNER JOIN wp_term_relationships rl1 ON (
    p.ID = rl1.object_id
)
INNER JOIN wp_term_taxonomy rl2 ON (
    rl1.term_taxonomy_id = rl2.term_taxonomy_id
)
INNER JOIN wp_terms ct ON (
    rl2.term_id = ct.term_id AND
    ct.term_id = 795 OR
    ct.term_id = 846
)
GROUP BY p.ID";