Error in mysql php query

0

I have an error which I have not been able to solve, it gives the expected results but at the end I get an error.

This is my code:

<?php

require('datos.php');

$conexion=mysqli_connect($db_host,$db_usuario,$db_contra,$db_nombre);

if($conexion) {
    if($_POST) {    

        $Cedula=$_POST['cedula'];
        $consulta_existe ="SELECT * FROM clientes WHERE cedula='$Cedula'";
        $resultados_existe=mysqli_query($conexion,$consulta_existe);

        if($resultados_existe==1) {
            echo "<script type='text/javascript'> alert('No existe.'); </script>";
        } else {
            echo "<script type='text/javascript'> alert('El cliente ya esta registrado.'); </script>";  
        }

    }
}

?>

    
asked by Javier 09.08.2018 в 02:25
source

1 answer

0

Following what you need and the instructions of Maestro A. Cedano, I leave you a possible solution to your problem using sentences prepared to avoid the injection of sql:

<?php

require('datos.php');

//creamos una instancia
$mysqli = new mysqli($db_host,$db_usuario,$db_contra,$db_nombre);
//verificamos que no hayan errores en la conexion
if(!$mysqli->connect_errno){
  //validamos que los datos llegue y que no esten vacios
  if(isset($_POST['cedula']) && !empty($_POST['cedula']))  {   

    //capturamos la variable del post
    $Cedula=$_POST['cedula'];
    //preparamos la sentencia con el simbolo "?"
    $sql = $mysqli->prepare("SELECT * FROM clientes WHERE cedula=$Cedula");
    //enviamos el parametro que preparamos 
    $sql->bind_param("i", $Cedula)
    //ejecutamos la sentencia
    $sql->execute();
    //obtenemos el resultado
    $resultado = $sql->get_result();
    //sacamos el numero de filas que trae
    $fila = $resultado->fetch_assoc();
    //validamos que si haya traido por lo menos una fila
    if($fila>0) {
      echo "<script type='text/javascript'> alert('El cliente ya esta registrado.'); </script>";  
    }
    else    {
      echo "<script type='text/javascript'> alert('No existe.'); </script>";
    }



  }
  else{
    //validamos si no llegaron los datos
    echo "<script type='text/javascript'> alert('No llegaron los Datos'); </script>"; 
  }
}
else{
  //mostramos alerta por si fallo la conexion
  echo "<script type='text/javascript'> alert('Fallo la conexion a la BD'); </script>"; 
 }


?>

I hope you serve and lean on this here I leave the link Handbook of sentences prepared successful bro. ReNiceCode ...

    
answered by 09.08.2018 в 03:41