I have these values and still do not let me do the insert

1
            $sql = "INSERT INTO usuario (";
    $sql .= "CUM, Nombre, A_Pat, A_Mat, Sexo, F_Naci, Vigencia, Provincia, Grupo_S, Seccion, Scouter_Responsable,";
    $sql .= "Tel_Cel, Password, Estatura, Peso, Color_Cabello, Color_Ojos, Color_Piel, Tipo_Sangre, Donar_Sangre, Dieta, Alcohol,";
    $sql .= "Fumador, Uso_Sustancias,Fam_Diabetes, Fam_Hipertension, Afecciones_Frio_Calor, Fk_Info_Emergencia, Fk_Regnal, Tipo_Usuario";
    $sql .= ") VALUES (";
    $sql .= ":cum,'".$this->Nombre."', :ap_pa, :ap_ma, :sexo,'".$this->F_Nac."','".$this->Vigencia."',:provincia,:grupo,:seccion,:scouterR,:celular,:passuser,:estatura,";
    $sql .= ":peso,'".$this->Color_Cabello."',:color_ojos,:color_piel,'".$this->Tipo_Sangre."',:donarsangre,:dieta,'".$this->Alcohol."',:fuma,:sustanciasrecreativas,'".$this->Fam_Diabetes."',";
    $sql .= ":familiahipertension,:afecciones,'1','jalo000001',:tipouser)";
    $consulta = $conexion->prepare($sql);
    $consulta->bindParam(':cum', $this->CUM);//1
    $consulta->bindParam(':nombre', $this->Nombre);//2
    $consulta->bindParam(':ap_pa', $this->A_Pat);//3
    $consulta->bindParam(':ap_ma', $this->A_Mat);//4
    $consulta->bindParam(':sexo', $this->Sexo);//5
    $consulta->bindParam(':fechadenacimiento', $this->F_Nac);//6
    $consulta->bindParam(':vigenciascout', $this->Vigencia);//7
    $consulta->bindParam(':provincia', $this->Provincia);//8
    $consulta->bindParam(':grupo', $this->Grupo_S);//9
    $consulta->bindParam(':seccion', $this->Seccion);//10
    $consulta->bindParam(':scouterR', $this->Scouter_Responsable);//11
    $consulta->bindParam(':celular', $this->Tel_Cel);//12
    $consulta->bindParam(':passuser', $this->Password);//13
    $consulta->bindParam(':estatura', $this->Estatura);//14
    $consulta->bindParam(':peso', $this->Peso);//15
    $consulta->bindParam(':color_cabello', $this->Color_Cabello);//16
    $consulta->bindParam(':color_ojos', $this->Color_Ojos);//17
    $consulta->bindParam(':color_piel', $this->Color_Piel);//18
    $consulta->bindParam(':tiposangre', $this->Tipo_Sangre);//19
    $consulta->bindParam(':donarsangre', $this->Donar_Sangre);//20
    $consulta->bindParam(':dieta', $this->Dieta);//21
    $consulta->bindParam(':bebealcohol', $this->Alcohol);//22
    $consulta->bindParam(':fuma', $this->Fumador);//23
    $consulta->bindParam(':sustanciasrecreativas', $this->Uso_Sustancias);//24
    $consulta->bindParam(':familiadiabetes', $this->Fam_Diabetes);//25
    $consulta->bindParam(':familiahipertension', $this->Fam_Hipertension);//26
    $consulta->bindParam(':afecciones', $this->Afecciones_Frio_Calor);//27
    //$consulta->bindParam(':infoemerg', '1');
    //$consulta->bindParam(':regnalito', 'JAL0290507');
    $consulta->bindParam(':tipouser', $this->Tipo_Usuario);//28
   // var_dump($consulta);

    if (!$consulta || !$consultaEmergencia)
    {
        $this->mensaje = $mensaje = $conexion->errorInfo();
    }else {
        //$consultaEmergencia->execute();
        $consulta->execute();
        $this->mensaje = "Se hizo el insert";
    }
    
asked by Ernesto Vazquez 14.11.2018 в 02:37
source

1 answer

2

Welcome to Stackoverflow.

This part of your query is totally wrong:

$sql .= ":cum,'".$this->Nombre."', :ap_pa, :ap_ma, :sexo,'".$this->F_Nac."','".$this->Vigencia."',:provincia,:grupo,:seccion,:scouterR,:celular,:passuser,:estatura,";
$sql .= ":peso,'".$this->Color_Cabello."',:color_ojos,:color_piel,'".$this->Tipo_Sangre."',:donarsangre,:dieta,'".$this->Alcohol."',:fuma,:sustanciasrecreativas,'".$this->Fam_Diabetes."',";
$sql .= ":familiahipertension,:afecciones,'1','jalo000001',:tipouser)";

In prepared queries you can not do things like this:

":cum,'".$this->Nombre."',

The markers must be separate, one after another and the values apart, either by a method bind , or in the execute (since PDO offers the advantage of passing the data in the form of an array in execute which is an interesting advantage, especially for cases like yours).

Since the query is very long and you can go wrong with a :nombre marker, you can use position markers ? , and pass the data as an array in execute . That way there is less risk of error. What you should verify is that the number of markers coincide exactly and that in the array they have the corresponding positions.

I propose to modify the code like this:

/*Consulta lo más sencilla posible con marcadores de posición*/
$sql = 
    "INSERT INTO usuario 
        (
            CUM, 
            Nombre, 
            A_Pat, 
            A_Mat, 
            Sexo, 
            F_Naci, 
            Vigencia, 
            Provincia, 
            Grupo_S, 
            Seccion, 
            Scouter_Responsable,
            Tel_Cel, 
            Password, 
            Estatura, 
            Peso, 
            Color_Cabello, 
            Color_Ojos, 
            Color_Piel, 
            Tipo_Sangre, 
            Donar_Sangre, 
            Dieta, 
            Alcohol,
            Fumador, 
            Uso_Sustancias,
            Fam_Diabetes, 
            Fam_Hipertension, 
            Afecciones_Frio_Calor, 
            Fk_Info_Emergencia, 
            Fk_Regnal, 
            Tipo_Usuario
        )
    VALUES 
        (
            ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
        )
    ";



    if (!$consulta || !$consultaEmergencia)  
    {
        $this->mensaje = $mensaje = $conexion->errorInfo();
    }else {

    /*Datos que pasaremos en el execute*/   
    $arrParams=array 
            (
                $this->CUM,
                $this->Nombre,
                $this->A_Pat,
                $this->A_Mat,
                $this->Sexo,
                $this->F_Nac,
                $this->Vigencia,
                $this->Provincia,
                $this->Grupo_S,
                $this->Seccion,
                $this->Scouter_Responsable,
                $this->Tel_Cel,
                $this->Password,
                $this->Estatura,
                $this->Peso,
                $this->Color_Cabello,
                $this->Color_Ojos,
                $this->Color_Piel,
                $this->Tipo_Sangre,
                $this->Donar_Sangre,
                $this->Dieta,
                $this->Alcohol,
                $this->Fumador,
                $this->Uso_Sustancias,
                $this->Fam_Diabetes,
                $this->Fam_Hipertension,
                $this->Afecciones_Frio_Calor,
                '1',
                'JAL0290507',
                $this->Tipo_Usuario
            );

        $consulta->execute($arrParams);
        $this->mensaje = "Se hizo el insert"; //deberías verificar con affected_rows
    }
    
answered by 14.11.2018 / 03:20
source