mysqli_stmt_bind_param () expects parameter 1 to be mysqli_stmt, boolean given?

0

I have my php code for the connection to the database but it sends me that error on line 13 and 14, I can not find a solution, I attach my code

$con = mysqli_connect("localhost", "root", "","SaveMe");
$Usuario = $_POST["Usuario"];
$Contraseña = $_POST["Contraseña"];
$Nombre = $_POST["Nombres"];
$Apellido = $_POST["Apellidos"];
$Edad = $_POST["Edad"];
$Numero = $_POST["Numero"];
$Correo = $_POST["Correo"];
linea 13  $statement = mysqli_prepare($con, "INSERT INTO usuarios (Usuario, Contraseña, Nombres, Apellidos, Edad, Numero, Correo) VALUES (?, ?, ?, ?, ?, ?, ?)");
linea 14 mysqli_stmt_bind_param($statement, "ssssiis", $Usuario, $Contraseña, $Nombre, $Apellido, $Edad, $Numero, $Correo);
mysqli_stmt_execute($statement);

$response = array();
$response["success"] = true;  

echo json_encode($response);
    
asked by Pedro Lizarraga 10.10.2017 в 21:08
source

1 answer

0

When programs , two things can happen: either you control the code, or the code controls you.

It is always good to opt for the second option, establishing strict control of the code, so that you always know what is happening.

Here we go:

$con = mysqli_connect("localhost", "root", "","SaveMe");

/*
    *Control 0 (pendiente): Sería bueno que controles si las variables del POST tienen datos.
    *No lo he hecho porque la tabla puede admitir algunos valores en blanco
    *Al menos debes controlar que aquellos valores esenciales no estén vacíos
*/
$Usuario = $_POST["Usuario"];
$Contraseña = $_POST["Contraseña"]; //Yo no usaría la ñ, podría dar problema en algún escenario
$Nombre = $_POST["Nombres"];
$Apellido = $_POST["Apellidos"];
$Edad = $_POST["Edad"];
$Numero = $_POST["Numero"];
$Correo = $_POST["Correo"];

/*
    *Declaramos $response aquí para usarla ocurra lo que ocurra
    *Si hay un error le haremos decir a $response lo que ocurre
*/
$response = array();

/*Empezamos a controlar el código*/

/*Control 1: debes saber lo que ocurre con la conexión*/

if ($con){
    /*Prefiero variables para todo, me parece que es más limpio, es algo personal*/
    $sql="INSERT INTO usuarios (Usuario, Contraseña, Nombres, Apellidos, Edad, Numero, Correo) VALUES (?, ?, ?, ?, ?, ?, ?)";
    $statement = mysqli_prepare($con, $sql);

    /*Control 2: ¿El $statement se preparó o no?*/
    if ($statement) {
        mysqli_stmt_bind_param($statement, "ssssiis", $Usuario, $Contraseña, $Nombre, $Apellido, $Edad, $Numero, $Correo);
        mysqli_stmt_execute($statement);

        /*Control 3: ¿Qué pasó en la ejecución?*/

        $contador=mysqli_stmt_affected_rows($statement);

        if ($contador){         
            $response["success"] = true;  
            $response["mensaje"] = "Se insertaron ".$contador." registros";  

        }else{
            $mensaje=mysqli_stmt_error($statement);     
            $response["success"] = false;  
            $response["mensaje"] = "Error insertando. Datos duplicados o algún dato erróneo: ".$mensaje;  

        }

        /* cerrar sentencia */
        mysqli_stmt_close($statement);

    }else{
        $mensaje=mysqli_stmt_error($statement);     
        $response["success"] = false;  
        $response["mensaje"] = "Error preparando la consulta SQL: ".$mensaje;  

    }   

    /* cerrar conexión (si es preciso)*/
    mysqli_close($con);

}else{
    $mensaje=mysqli_connect_errno().PHP_EOL. mysqli_connect_error();
    $response["success"] = false;  
    $response["mensaje"] = $mensaje;  

}
echo json_encode($response);

I explained everything in the comments. Now this line, put at the end of the whole: echo json_encode($response); will tell you always what is happening with your code.

    
answered by 11.10.2017 / 01:20
source