Search by number in Mysql table with PHP

Asked

Viewed 180 times

0

I have a search form where the user type a number and if it is equal to the number in the database it returns the data of that number. But when I click on button enviar he shows me the page teste.php with the following error (You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near '232' at line 1) follows the code.

Database:

CREATE TABLE `tb_numeros` (
  `numero` int(11) NOT NULL,
  `funcionario` varchar(120) NOT NULL,
  `rca` int(4) NOT NULL,
  `regiao` varchar(14) NOT NULL,
  `nchip` int(120) NOT NULL,
  `imei` int(120) NOT NULL
) PRIMARY KEY (`numero`);

Form:

<form class="navbar-form navbar-left" method="post" action="teste.php" role="search">
    <div class="form-group">
        <input type="text" name="numerodigitado" class="form-control" placeholder="Buscar Número">
    </div>
        <button type="submit" name="enviar" class="btn btn-default"><i class="glyphicon glyphicon-search"></i></button>
</form>

php test.:

<?php
include_once("../conn/conexao.php");//faz a conexao com o banco de dados

    if(isset($_POST['numerodigitado'])){
        $palavra = trim($_POST['numerodigitado']);
    }else{
        $palavra = trim($_GET['numerodigitado']);
    }

    if($palavra != NULL){
        // Retorna apenas os registros PARECIDOS com 'palavra', limitando à 10 registros por página
        $sql = "SELECT * FROM tb_numeros WHERE numero LIKE '%$palavra%' ";
    }else{
        // Retorna todos os registros do BD
        $sql = "SELECT * FROM tb_numeros ORDER BY numero ";
    }

    $comparar = mysqli_query($conexao, $palavra) or die (mysqli_error($conexao));
    if($comparar):
    echo "<script>
            alert('Número encontrado.');

        </script>"; 
    else:
        echo "<script>
                alert('Ocorreu um erro ao mostrar o número.');

            </script>";
    endif;

?>
  • 1

    But your PHP file does nothing at all. The only thing you do is set the value of $palavra and of $sql, but you never run the query in the database and do not display any results on the screen. There was no missing one mysqli_query to run the query and iterate on the results? If you have no idea what I said, it might be interesting for you to reread the documentation.

  • $compare = mysqli_query($connected, $word) or die (mysqli_error($connected)); I created this field and an if for $compare but it is showing a following error: You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax

  • Then edit the question, enter the full code and the error message. This is essential information to verify the problem and should not be omitted.

  • edited there friend

  • $compare = mysqli_query($connected, $sql) and not $word. This is the error...

1 answer

1


Remembering that below I will use a connection variable, called $Conn, exchange for your variable created to make the connection in the database

<?php

  include_once("../conn/conexao.php");//faz a conexao com o banco de dados

  if(isset($_POST['numerodigitado'])){

    $numerodigitado = $_POST['numerodigitado'];

    $result = "SELECT * FROM tb_numeros WHERE numero = '$numerodigitado' ";
    $resultado = mysqli_query($conn, $result);
    $row = mysqli_fetch_assoc($resultado);

    if($resultado -> num_rows > 0){
       echo " .$row['campo1']. ";
       echo " .$row['campo2']. ";
       echo " .$row['campo3']. ";
    } else {
       echo "<script>alert('Ocorreu um erro ao buscar o número');</script>";
    }

?>
  • I’m trying to help, but I’m in the service and very busy, they’re interrupting me too much and I ended up mixing the code I believe. could edit the answer ?

  • he printed it : SELECT * FROM tb_numeros WHERE numero LIKE '%232%'

  • continues showing nothing, I want that if the number typed is the same as the one registered in the database it shows the data that are registered in this number... so far only shows a blank page...

  • @Ferb check now with this code, and remember inside where you are field1, field2, field3 put the fields referring to the database

  • 1

    Thanks, that worked out just fine

Browser other questions tagged

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