Search in php does not return the results correctly

Asked

Viewed 62 times

-1

I have a problem regarding the return of the database information in php, I have two tables, a tb_regiao, with the Brazilian states and another, tb_representative with representatives in each state.

Below is the part with select:

 <form class="ajs-form-representante" method="GET" action="pesquisar_representantes_art_ferro_moveis_fibra_sintetica.php">
            <select type="text" name="pesquisar" >
                <option class="lista-cidade" value="" selected="selected">Escolha a Região</option>
                <?php require_once ("ajs-script-php/selecta_cidade.php");?>
            </select>
            <input type="submit" value="BUSCAR">
        </form>

The script to select the region is this below:

<?php
require_once("ajs-adm/conexao/conecta.php");

$pesqu = $_GET["pesquisar"];
$listar = $conexao->prepare('SELECT * FROM tb_regiao WHERE regiao LIKE "%'.$pesqu.'%" LIMIT 27');
$listar->execute();
while ($exibe = $listar->fetch()){
    $nome_cidade = utf8_encode($exibe['regiao']);
    $nome_id = utf8_encode($exibe['id_id_reg']);
    echo "<option  class='$nome_id'>$nome_cidade</option>";
}

The page code to display the result is this below:

        <section class="ajs-section-rs-a">
            <?php
                $pesquisar = $_GET['pesquisar'];
                $result_repres = "SELECT * FROM tb_representante WHERE estado LIKE '%".$pesquisar."%' LIMIT 1";
                $result_resul_repres = mysqli_query($conect, $result_repres);
                   while($rows_repres = mysqli_fetch_array($result_resul_repres)){
                       echo "<p><b>Representante:</b> ".utf8_encode($rows_repres['nome_rp'])."</p>";
                       echo "<p><b>E-mail:</b> ".utf8_encode($rows_repres['email'])."</p>";
                       echo "<p><b>Celular:</b> ".utf8_encode($rows_repres['cel_a'])."</p>";
                       echo "<p><b>Celular:</b> ".utf8_encode($rows_repres['cel_b'])."</p>";
                       echo "<p><b>Telefone Fixo:</b> ".utf8_encode($rows_repres['tel_fixo'])."</p>";
                       echo "<p><b>Empresa:</b> ".utf8_encode($rows_repres['nome_empresa'])."</p>";
                       echo "<p><b>Endereço:</b> ".utf8_encode($rows_repres['rua'])."</p>";
                       echo "<p><b>Bairro:</b> ".utf8_encode($rows_repres['bairro'])."</p>";
                       echo "<p><b>Cidade:</b> ".utf8_encode($rows_repres['cidade'])."</p>";
                       echo "<p><b>Cep:</b> ".utf8_encode($rows_repres['cep'])."</p>";
                       echo "<p><b>Estado:</b> ".utf8_encode($rows_repres['estado'])."</p>";
                   }
            ?>
        </section>

below is the table tb_regiao:

--

CREATE TABLE `tb_regiao` (
  `id_reg` int(11) NOT NULL,
  `id_id_reg` varchar(2) COLLATE utf8_unicode_ci NOT NULL,
  `regiao` varchar(30) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Extraindo dados da tabela `tb_regiao`
--

INSERT INTO `tb_regiao` (`id_reg`, `id_id_reg`, `regiao`) VALUES
(1, '1', 'Acre'),
(2, '2', 'Alagoas'),
(3, '3', 'Amapá'),
(4, '4', 'Amazonas'),
(5, '5', 'Bahia'),
(6, '6', 'Ceará'),
(7, '7', 'Distrito Federal'),
(8, '8', 'Espírito Santo'),
(9, '9', 'Goiás'),
(10, '10', 'Maranhão'),
(11, '11', 'Mato Grosso'),
(12, '12', 'Mato Grosso do Sul'),
(13, '13', 'Minas Gerais'),
(14, '14', 'Pará'),
(15, '15', 'Paraíba'),
(16, '16', 'Paraná'),
(17, '17', 'Pernambuco'),
(18, '18', 'Piauí'),
(19, '19', 'Rio de Janeiro'),
(20, '20', 'Rio Grande do Norte'),
(21, '21', 'Rio Grande do Sul'),
(22, '22', 'Rondônia'),
(23, '23', 'Roraima'),
(24, '24', 'Santa Catarina'),
(25, '25', 'São Paulo'),
(26, '26', 'Sergipe'),
(27, '27', 'Tocantins');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `tb_regiao`
--
ALTER TABLE `tb_regiao`
  ADD PRIMARY KEY (`id_reg`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tb_regiao`
--
ALTER TABLE `tb_regiao`
  MODIFY `id_reg` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=28;COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

And below is the tb_representative table:

--

CREATE TABLE `tb_representante` (
  `id` int(11) NOT NULL,
  `id_id_reg` varchar(2) COLLATE utf8_unicode_ci NOT NULL,
  `nome_rp` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `nome_one` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `nome_two` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `nome_three` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `nome_empresa` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `tel_fixo` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `cel_a` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `cel_b` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `rua` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `bairro` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `cidade` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `cep` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
  `estado` varchar(40) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Extraindo dados da tabela `tb_representante`
--

INSERT INTO `tb_representante` (`id`, `id_id_reg`, `nome_rp`, `email`, `nome_one`, `nome_two`, `nome_three`, `nome_empresa`, `tel_fixo`, `cel_a`, `cel_b`, `rua`, `bairro`, `cidade`, `cep`, `estado`) VALUES
(1, '9', 'Alamo Manini Representações', '[email protected]', 'Elaine', 'Ataíde', 'Deivide', 'Alamo Manini Representações', '(62) 3515-1110', '(62) 99945-0318', '(62) 98252-1518', 'Rua 6, nº 370 Quadra E3, Lote 31, Sala 910', 'Empire Center, setor oeste', 'Goiânia', '74.115-070', 'Goiás'),
(2, '5', 'Caires Representações', '[email protected]', 'Edvando', 'Zilmz', '', 'Caires Representações', '(77) 3441-2728', '(77) 99997-7885', '(77) 99996-3581', 'Praça dr. Nelson Lula, nº 22', 'Centro', 'Brumado', '46100-000', 'Bahia');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `tb_representante`
--
ALTER TABLE `tb_representante`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tb_representante`
--
ALTER TABLE `tb_representante`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

When I click to fetch the result, only the second record is returned, it does not show the first and no other.

  • pq has this one LIMIT 1 in the page code to display the result!?!

  • at first I thought it was necessary to limit the return of a result per region, but I did other tests and does not influence this type

  • in this example I sent that I use, it returns the result of the correct Bahia, but when I click in Goiás, or any other region, it does not return any value

  • 1

    If you’re gonna wear one select to choose the region, you can put the value of the select already as the region id, not to have to give a LIKE.

  • I’ll change here and test

  • found the error, with this tip of yours, in the script to select the region, I put class='$name_id', when you wrote above, "put the value" I looked and changed to "value='$name_id'", and ready, ran normal, who wants to use the code just change that runs 100%

  • @Anildofabianoalexandre put as answer, that way other people will know that the question has been solved

  • thanks for the tip Denis,

Show 3 more comments

1 answer

0


found the error, with this tip of yours, in the script to select the region, I put class='$name_id', when you wrote above, "put the value" I looked and changed to "value='$name_id'", and ready, ran normal, who wants to use the code just change that runs 100%`

this is the corrected script

<?php
require_once("ajs-adm/conexao/conecta.php");

$pesqu = $_GET["pesquisar"];
$listar = $conexao->prepare('SELECT * FROM tb_regiao WHERE regiao LIKE "%'.$pesqu.'%" LIMIT 27');
$listar->execute();
while ($exibe = $listar->fetch()){
    $nome_cidade = utf8_encode($exibe['regiao']);
    $nome_id = utf8_encode($exibe['id_id_reg']);
    echo "<option  value='$nome_id'>$nome_cidade</option>";
}

Browser other questions tagged

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