Warning: mysqli_stmt_bind_param (): Number of elements in type definition string does not match number of bind variables in?

0

Hi, I'm new to this php, please help me please, these are the errors that are thrown

  

Notice: Undefined index: name in C: \ xampp \ htdocs \ Register.php on line   4

     

Notice: Undefined index: course in C: \ xampp \ htdocs \ Register.php on   line 5

     

Notice: Undefined index: surname in C: \ xampp \ htdocs \ Register.php on   line 6

     

Notice: Undefined index: user_name in C: \ xampp \ htdocs \ Register.php on   line 7

     

Notice: Undefined index: password in C: \ xampp \ htdocs \ Register.php on   line 8

     

Warning: mysqli_stmt_bind_param (): Number of elements in type   definition string does not match number of bind variables in   C: \ xampp \ htdocs \ Register.php on line 10 {"success": true}

and this is my code:

{<?php
    $con = mysqli_connect("localhost", "root", "", "usuarios");

    $name = $_POST["name"];
    $course = $_POST["course"];
    $surname = $_POST["surname"];
    $user_name = $_POST["user_name"];
    $password = $_POST["password"];
    $statement = mysqli_prepare($con, "INSERT INTO usuarios(name,course,surname,user_name,password) VALUES (?, ?, ?, ?, ?)");
    mysqli_stmt_bind_param($statement, "ssis", $name, $user_name, $course, $surname, $password);
    mysqli_stmt_execute($statement);

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

    echo json_encode($response);
?>}

this is from the other file

  

Notice: Undefined index: user_name in C: \ xampp \ htdocs \ Login.php on   line 4

     

Notice: Undefined index: password in C: \ xampp \ htdocs \ Login.php on line   5

     

Warning: mysqli_stmt_bind_param () expects parameter 1 to be   mysqli_stmt, boolean given in C: \ xampp \ htdocs \ Login.php on line 8

     

Warning: mysqli_stmt_execute () expects parameter 1 to be mysqli_stmt,   boolean given in C: \ xampp \ htdocs \ Login.php on line 9

     

Warning: mysqli_stmt_store_result () expects parameter 1 to be   mysqli_stmt, boolean given in C: \ xampp \ htdocs \ Login.php on line 11

     

Warning: mysqli_stmt_bind_result () expects parameter 1 to be   mysqli_stmt, boolean given in C: \ xampp \ htdocs \ Login.php on line 12

     

Warning: mysqli_stmt_fetch () expects parameter 1 to be mysqli_stmt,   boolean given in C: \ xampp \ htdocs \ Login.php on line 17   {"success": false}

code:

{<?php
    $con = mysqli_connect("localhost", "root", "", "usuarios");

    $user_name = $_POST["user_name"];
    $password = $_POST["password"];

    $statement = mysqli_prepare($con, "SELECT * FROM user WHERE user_name = ? AND password = ?");
    mysqli_stmt_bind_param($statement, "ss", $user_name, $password);
    mysqli_stmt_execute($statement);

    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement, $userID, $name, $course, $user_name,$surname, $password);

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

    while(mysqli_stmt_fetch($statement)){
        $response["success"] = true;  
        $response["name"] = $name;
    $response["course"] = $course;
        $response["surname"] = $surname;
        $response["user_name"] = $user_name;
        $response["password"] = $password;
    }

    echo json_encode($response);
?>}

thanks

    
asked by Briant Ayala 30.10.2018 в 06:56
source

1 answer

2

Welcome to Stackoverflow.

Your error seems to be in this line:

mysqli_stmt_bind_param($statement, "ssis", $name, $user_name, $course, $surname, $password);

In the data type indicators ( "ssis" ), you are passing only four, but they must be five.

Assuming that the only numeric there is $course , it is clear that you have to put the line like this:

mysqli_stmt_bind_param($statement, "ssiss", $name, $user_name, $course, $surname, $password);

//                                  12345
// 1. $name  
// 2. $user_name   
// 3. $course
// 4. $surname
// 5. $password

Code optimization note

Your insertion code does not verify that there really has been an insertion. Errors can still occur with all the correct code, for example, the insert can violate a restriction of the DB that would prevent a new record from being added. You would be returning TRUE in that case of no insertion.

So generally the real inserts should be controlled with mysqli_affected_rows .

For example, here we only return TRUE if there were really insertions:

//... todo lo anterior
mysqli_stmt_execute($statement);
//Contamos cuantas filas se insertaron y lo guardamos en una variable por si lo necesitamos
$count=mysqli_affected_rows($con);
//Usamos un ternario para devolver true si hubo inserciones solamente
$status=($count > 1) ? TRUE : FALSE;

//simplificamos
$response = array("success" => $status);

echo json_encode($response);

More things can be done, such as returning the reason for the error for which there were no insertions, returning the number of rows, etc. That already depends on the context of your program.

    
answered by 30.10.2018 в 12:27