MySql statement is not executed in php function

0

Friends I'm new to php .. I have a mysql statement that does not give me a php function, I execute it in mysql and if it executes me in the function it does not. Also the data arrives at the function and all I do not do is run the mysql semtencia

function RegistrarUsuario($smallnameusuario,$contraseñausuario)
{
    $mensaje = "";

    if(isset($smallnameusuario,$contraseñausuario))
    {

        $tipoUsuario = 1;
        //$currDate = getDate(); // generas un llamado al metodo

        $fecharegistro = date("Y-m-d");

        $idpersona = ObtenerUltimaPersona();
        $eliminadologico = 1;

        echo "Usuario: ".$smallnameusuario.", ";
        echo "Contraseña: ".$contraseñausuario.", ";
        echo "Tipo: ".$tipoUsuario.", ";
        echo "Fecha: ".$fecharegistro.", ";
        echo "ID Persona: ".$idpersona.", ";
        echo "Eliminado logico: ".$eliminadologico.". ";


        $query2 = "INSERT INTO registro(idpersona, idtipousuario, nombreusuario, contraseña, fecharegistro, eliminadologicor) VALUES ('".$idpersona."','".$tipoUsuario."','".$smallnameusuario."','".$contraseñausuario."','".$fecharegistro."','".$eliminadologico."')";

    $con2 = conexionBD();
    $accion2 = mysqli_query($con2,$query2);

    if($accion2)
    {
        $mensaje= "Correcto";
    }else
    {
        $mensaje= "No se pudo ejecutar la acción";

    }



    }else
    {
        $mensaje = "Los datos no fueron recibidos correctamente";
    }


    echo $mensaje;
    return $mensaje;
}

The echos that are there I put them in to confirm that I was entering the function. the idpersona is obtained with another function that executes.

    
asked by Tevin Lectong 04.06.2018 в 23:37
source

1 answer

1

Here you have a code implementing prepared queries.

I have commented the most important parts, explaining how to build the query ( A ) and pass the parameters ( B ).

I have allowed myself to use more descriptive variables, which help to read / understand the code. It is never recommended to use variables of type $con2, query2... . When the program grows and you see those variables you will have no idea what they do.

I have used a convention of variable names in English, because it is much easier and practical, although it is programmed in Spanish. In addition, it will help you not to be tempted to write variable names with ñ or with accents ... In many places they recommend using English because it is more universal. This is a matter of taste.

The code would look something like this:

function RegistrarUsuario($usrSmallName,$usrPass)
{
    $mensaje = "";

    if(isset($usrSmallName,$usrPass))
    {
        $usrType = 1;
        //$currDate = getDate(); // generas un llamado al metodo
        $usrDate = date("Y-m-d");
        $usrID = ObtenerUltimaPersona();
        $usrDelete = 1;
        echo "Usuario: ".$usrSmallName.", ";
        echo "Contraseña: ".$usrPass.", ";
        echo "Tipo: ".$usrType.", ";
        echo "Fecha: ".$usrDate.", ";
        echo "ID Persona: ".$usrID.", ";
        echo "Eliminado logico: ".$usrDelete.". ";

        /*
            *(A) 1er paso para consultas preparadas
            *En vez de pasar los datos directamente a la consulta
            *se usan marcadores de posición ?
            *y los valores en sí son pasados por el método mysqli_stmt_bind_param
            *Esto evita que se cuele código malicioso o manipulado
        */
        $usrInsert = "INSERT INTO registro(idpersona, idtipousuario, nombreusuario, contrasena, fecharegistro, eliminadologicor)
                               VALUES (?,?,?,?,?,?)";
        $con = conexionBD();
        if ($stmt = mysqli_prepare($con, $usrInsert))
        {
            /* 
                *(B) como se dijo en (A)
                *aquí pasamos cada valor al statement
                *Hay que tener en cuenta que:
                * - las letras "iisssi" corresponden el tipo de dato de cada valor (i)nteger, (s)tring
                * - cada tipo de dato debe ir en el orden en que está cada columna en $usrInsert
                * - cada variable debe pasarse en el orden respectivo a cada columna en $usrInsert
                * - en $usrInsert debe haber tantos marcadores de posicion ? como columnas
            */
            mysqli_stmt_bind_param($stmt, "iisssi", $usrSmallName,$usrPass,$usrType,$usrDate,$usrID,$usrDelete);
            /* ejecutar la consulta */
            mysqli_stmt_execute($stmt);
            $mensaje="Correcto. Se insertaron ".mysqli_stmt_affected_rows($stmt)." registros";
        }else
        {
            $mensaje= "No se pudo ejecutar la acción. Ocurrió el error: ".mysqli_error($con);
        }
    }else
    {
        $mensaje = "Los datos no fueron recibidos correctamente";
    }
    echo $mensaje;
    return $mensaje;
}
    
answered by 13.06.2018 в 12:20