PDO error: Uncaught PDOException: SQLSTATE [HY093]: Invalid parameter

0

This is my code:

$errors = array();
$data = array();

if ((empty($_POST['nombre']))||(ctype_space($_POST['nombre'])))
    $errors['nombre'] = 'Nombre (s)';

if ((empty($_POST['apellidoP']))||(ctype_space($_POST['apellidoP'])))
    $errors['apellidoP'] = 'Apellido Paterno';

if ((empty($_POST['apellidoM']))||(ctype_space($_POST['apellidoM'])))
    $errors['apellidoM'] = 'Apellido Materno';

if ((empty($_POST['direccion']))||(ctype_space($_POST['direccion'])))
    $errors['direccion'] = 'Dirección';

if ((empty($_POST['usuario']))||(ctype_space($_POST['usuario'])))
    $errors['usuario'] = 'Usuario';

if ((empty($_POST['contrasenia']))||(ctype_space($_POST['contrasenia'])))
    $errors['contrasenia'] = 'Contraseña';

if ((empty($_POST['rfc']))||(ctype_space($_POST['rfc'])))
    $errors['rfc'] = 'RFC';

if ((empty($_POST['telefono']))||(ctype_space($_POST['telefono'])))
    $errors['telefono'] = 'Teléfono';

if ((empty($_POST['email']))||(ctype_space($_POST['email'])))
    $errors['email'] = 'E-mail';

if ( !empty($errors)) {
    $data['success'] = false;
    $data['errors']  = $errors;
    $data['message']  = 'Verifique los siguientes datos: ';
    $data['info'] = 'error';
}
else
{   $nombre = $_POST['nombre'];
    $apellidoP = $_POST['apellidoP'];
    $apellidoM = $_POST['apellidoM'];
    $direccion = $_POST['direccion'];
    $usuario = $_POST['usuario'];
    $contrasenia = $_POST['contrasenia'];
    $rfc = $_POST['rfc'];
    $telefono = $_POST['telefono'];
    $email = $_POST['email'];

    if ((empty($_POST['activo']))||(ctype_space($_POST['activo']))){
        $activo = '0';}
    else{
            $activo = $_POST['activo'];
        }       
    $acceso = $_POST['nivel'];

    $db->where('usuario',$usuario); 
    $db->where('id_usuario',$_SESSION['idusu'],'<>','AND'); 
    $campos = "COUNT(usuario) as total ";
    $resultado = $db->get("usuario",null,$campos);
    if($resultado){
        foreach ($resultado as $key) {
            $total=$key['total'];
        }
    }
    if ($total>0){  
        $errors['usuario'] = 'Usuario';
        $data['success'] = false;
        $data['errors']  = $errors;
        $data['message'] = "Ya existe un usuario con este dato:"; 
        $data['info'] = 'error';
    }
    else{

        $sql = "UPDATE usuario SET 
                       nombre = :nombre,
                       app = :app,
                       apm = :apm,
                       direccion =:direccion,
                       usuario = :usuario,
                       contrasenia = :contrasenia,
                       rfc = :rfc,
                       celular = :telefono,
                       email = :mail,
                       estatus = :activo,
                       acceso = :nivel,
                       alta_usuario =:alta_usuario
                        WHERE id_usuario = :id_usuario";


        $stmt = $pdo->prepare($sql);                                  
        $stmt->bindParam(':nombre', $nombre, PDO::PARAM_STR);
        $stmt->bindParam(':app', $apellidoP, PDO::PARAM_STR);
        $stmt->bindParam(':apm', $apellidoM, PDO::PARAM_STR);
        $stmt->bindParam(':direccion', $direccion, PDO::PARAM_STR);
        $stmt->bindParam(':usuario', $usuario, PDO::PARAM_STR);
        $stmt->bindParam(':contrasenia', $contrasenia, PDO::PARAM_STR);
        $stmt->bindParam(':rfc', $rfc, PDO::PARAM_STR);
        $stmt->bindParam(':celular', $telefono, PDO::PARAM_STR);
        $stmt->bindParam(':email', $mail, PDO::PARAM_STR);
        $stmt->bindParam(':estatus', $activo, PDO::PARAM_INT);
        $stmt->bindParam(':acceso', $nivel, PDO::PARAM_INT);
        $stmt->bindParam(':alta_usuario', $alta_usuario, PDO::PARAM_STR);
        $stmt->bindParam(':id_usuario', $_SESSION['idusu'], PDO::PARAM_INT);
        $resultado = $stmt->execute();

        if($resultado>0)
        {
            $data['success'] = true;
            $data['message'] = "El usuario se ha modificado satisfactoriamente";  
            $data['id'] = $resultado;
        }
        else
        {
            $data['success'] = false;
            $data['message'] = 'Error en la modificaci&oacute;n del registro';
        }
    }
}   
break;

                                      

                 USER MODULE                     Enter the corresponding information.                 

                                                                                                                                  

MODIFY USER

                    </div>
                    <div class="body">
                        <form id="form_validation" method="post" action="ajaxusuarios.php" enctype="multipart/form-data">
                        <?php
                        $db->where('id_usuario',$_SESSION['usuario']);  
                        $campos = "usuario,contrasenia,acceso,estatus,nombre,app,apm,rfc,direccion,email,celular,alta_usuario";
                        $resultado = $db->get("usuario",null,$campos);//nombre de la tabla USUARIOS
                        if($resultado){
                            foreach ($resultado as $key) {
                        ?>
                        <div class="row clearfix">
                            <div class="col-sm-4">
                                <div class="form-group form-float">
                                    <div class="form-line">
                                        <input type="text" class="form-control" name='nombre' value="<?php echo $key['nombre'];?>" required/>
                                        <label class="form-label">Nombre</label>
                                    </div>
                                </div>
                            </div>
                            <div class="col-sm-4">
                                <div class="form-group form-float">
                                    <div class="form-line">
                                        <input type="app" class="form-control" name='apellidoP' value="<?php echo $key['app'];?>" required>
                                        <label class="form-label">Apellido Paterno</label>
                                    </div>
                                </div>
                            </div>
                            <div class="col-sm-4">
                                <div class="form-group form-float">
                                    <div class="form-line">
                                        <input type="app" class="form-control" name='apellidoM' value="<?php echo $key['apm'];?>" required>
                                        <label class="form-label">Apellido Maternos</label>
                                    </div>
                                </div>
                            </div>
                            </div>
                            <div class="form-group form-float">
                                <div class="form-line">
                                    <input type="text" class="form-control" name="direccion" value="<?php echo $key['direccion'];?>" required/>
                                    <label class="form-label">Dirección</label>
                                </div>
                            </div>
                        <div class="row clearfix">
                            <div class="col-sm-4">
                                <div class="form-group form-float">
                                    <div class="form-line">
                                        <input type="text" class="form-control" name='usuario' value="<?php echo $key['usuario'];?>" required/>
                                        <label class="form-label">Usuario</label>
                                    </div>
                                </div>
                            </div>
                            <div class="col-sm-4">
                                <div class="form-group form-float">
                                    <div class="form-line">
                                        <input type="password" class="form-control" name='contrasenia' value="<?php echo $key['contrasenia'];?>" required>
                                        <label class="form-label">Password</label>
                                    </div>
                                </div>
                            </div>
                            <div class="col-sm-4">
                                <!--<div class="form-group form-float">-->
                                    <div class="demo-switch"><b>Activo</b>&nbsp;&nbsp;
                                        <div class="switch">
                                            <label>No
                                            <?php if ($key['estatus']==1) {?>
                                                <input type="checkbox" name="estatus" value="1" checked/>
                                            <?php } else {?>
                                                <input type="checkbox" name="estatus" value="1" />
                                            <?php }?>   
                                            <span class="lever switch-col-<?php echo $_SESSION['color']; ?>"></span>Si</label>
                                        </div>
                                    </div>
                                <!--</div>-->
                            </div>
                         </div>
                        <div class="row clearfix">
                            <div class="col-md-4">
                                <b>Número Telefonico</b>
                                    <div class="input-group">
                                        <span class="input-group-addon">
                                            <i class="material-icons">phone</i>
                                        </span>
                                        <div class="form-line">
                                            <input type="text" class="form-control mobile-phone-number" placeholder="(000) 000-00-00" name="telefono"  value="<?php echo $key['celular'];?>" required>
                                        </div>
                                    </div>
                            </div>
                            <div class="col-md-4">
                                <b>Nivel de Acceso Administrador</b>
                                <div class="demo-radio-button">
                                    <?php if ($key['estatus']==1) {
                                        echo "<input name='nivel' type='radio' class='with-gap radio-col-".$_SESSION['color']."' id='radio_1' value='1' checked />";
                                    } else {
                                        echo "<input name='nivel' type='radio' class='with-gap radio-col-".$_SESSION['color']."' id='radio_1' value='1' />";
                                    } ?>
                                    <label for="radio_1">Seleccionar</label>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <b>Nivel de Acceso Programador</b>
                                <div class="demo-radio-button">
                                    <?php if ($key['estatus']==2) {
                                        echo "<input name='nivel' type='radio' class='with-gap radio-col-".$_SESSION['color']."' id='radio_2' value='2' checked/>";
                                    } else {
                                        echo "<input name='nivel' type='radio' class='with-gap radio-col-".$_SESSION['color']."' id='radio_2' value='2' />";
                                    } ?>
                                    <label for="radio_2">Seleccionar</label>
                                </div>
                            </div>
                        </div>
                        <div class="row clearfix">
                            <div class="col-md-4">
                                <b>RFC</b>
                                    <div class="input-group">
                                        <div class="form-line">
                                            <input type="text" class="form-control" placeholder="RFC" name="rfc"  value="<?php echo $key['rfc'];?>" required>
                                        </div>
                                    </div>
                            </div>
                            <div class="col-md-4">
                                <b>Nivel de Acceso Agende de Campo</b>
                                <div class="demo-radio-button">
                                    <?php if ($key['estatus']==3) {
                                        echo "<input name='nivel' type='radio' class='with-gap radio-col-".$_SESSION['color']."' id='radio_3' value='3' checked />";
                                    } else {
                                        echo "<input name='nivel' type='radio' class='with-gap radio-col-".$_SESSION['color']."' id='radio_3' value='3' />";
                                    } 

                                    ?>
                                    <label for="radio_3">Seleccionar</label>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <b>Nivel de Acceso Agente de Oficina</b>
                                <div class="demo-radio-button">
                                    <?php if ($key['estatus']==4) {
                                        echo "<input name='nivel' type='radio' class='with-gap radio-col-".$_SESSION['color']."' id='radio_4' value='4' checked/>";
                                    } else {
                                        echo "<input name='nivel' type='radio' class='with-gap radio-col-".$_SESSION['color']."' id='radio_4' value='4' />";
                                    } 

                                    ?>
                                    <label for="radio_4">Seleccionar</label>
                                </div>
                            </div>
                        </div>
                        <div class="row clearfix">
                            <div class="col-md-4">
                                <b>E-mail</b>
                                    <div class="input-group">
                                        <span class="input-group-addon">
                                            <i class="material-icons">mail</i>
                                        </span>
                                        <div class="form-line">
                                            <input type="text" class="form-control mail" placeholder="[email protected]" name="email"  value="<?php echo $key['email'];?>" required>
                                        </div>
                                    </div>
                            </div>
                        </div>


                         <?php }}?>

                            <divl class="col-xs-12">
                            <button class="btn btn-block btn-lg bg-<?php echo $_SESSION['color']; ?> waves-effect" type="submit"><b>Modificar</b></button>
                            </div>
                        </form>                            
                    </div>
                </div>
            </div>
        </div>

    </div>
</section>

This is the error message:

  

Fatal error: Uncaught PDOException: SQLSTATE [HY093]: Invalid parameter   number: parameter was not defined in / opt / lampp / htdocs / Agency   Nuba / Nubawork / work / ajaxusuarios.php: 216 Stack trace: # 0   / opt / lampp / htdocs / Nuba Agency / Nubawork / work / ajaxusuarios.php (216):   PDOStatement-> execute () # 1 {main} thrown in / opt / lampp / htdocs / Agency   Nuba / Nubawork / work / ajaxusuarios.php on line 216

    
asked by Rocío 28.04.2017 в 22:08
source

1 answer

2

You have an error in the assignment of variables, you have changed the name of several and they do not match, so the query can not be executed without them (hence the message "Invalid parameter number: parameter was not defined" or "number invalid parameter: parameter was not defined ", although it does not indicate the number of them):

app = :apellidoP,
apm = :apellidoM,
...
celular = :telefono,
email = :mail,
estatus = :activo,
acceso = :nivel,

They should be changed to:

app = :app,
apm = :apm,
...
celular = :celular,
email = :email,
estatus = :estatus,
acceso = :acceso,

To match with:

$stmt->bindParam(':app', $apellidoP, PDO::PARAM_STR);
$stmt->bindParam(':apm', $apellidoM, PDO::PARAM_STR);
$stmt->bindParam(':celular', $telefono, PDO::PARAM_STR);
$stmt->bindParam(':email', $mail, PDO::PARAM_STR);
$stmt->bindParam(':estatus', $activo, PDO::PARAM_INT);
$stmt->bindParam(':acceso', $nivel, PDO::PARAM_INT);

The code would be:

    $sql = "UPDATE usuario SET 
                   nombre = :nombre,
                   app = :app,
                   apm = :apm,
                   direccion =:direccion,
                   usuario = :usuario,
                   contrasenia = :contrasenia,
                   rfc = :rfc,
                   celular = :celular,
                   email = :email,
                   estatus = :estatus,
                   acceso = :acceso,
                   alta_usuario =:alta_usuario
                    WHERE id_usuario = :id_usuario";

    $stmt = $pdo->prepare($sql);                                  
    $stmt->bindParam(':nombre', $nombre, PDO::PARAM_STR);
    $stmt->bindParam(':app', $apellidoP, PDO::PARAM_STR);
    $stmt->bindParam(':apm', $apellidoM, PDO::PARAM_STR);
    $stmt->bindParam(':direccion', $direccion, PDO::PARAM_STR);
    $stmt->bindParam(':usuario', $usuario, PDO::PARAM_STR);
    $stmt->bindParam(':contrasenia', $contrasenia, PDO::PARAM_STR);
    $stmt->bindParam(':rfc', $rfc, PDO::PARAM_STR);
    $stmt->bindParam(':celular', $telefono, PDO::PARAM_STR);
    $stmt->bindParam(':email', $mail, PDO::PARAM_STR);
    $stmt->bindParam(':estatus', $activo, PDO::PARAM_INT);
    $stmt->bindParam(':acceso', $nivel, PDO::PARAM_INT);
    $stmt->bindParam(':alta_usuario', $alta_usuario, PDO::PARAM_STR);
    $stmt->bindParam(':id_usuario', $_SESSION['idusu'], PDO::PARAM_INT);
    $resultado = $stmt->execute();

Otherwise, nothing happens if the variable you assign is called $apellidoP instead of $app .

The important thing to remember is that for each variable that you create in the form :variable you must assign it a value with bindParam(':variable', $laVariableQueQueramos, PDO::...) .

    
answered by 28.04.2017 в 22:13