MYSQL query is not executed

1

I have not been able to solve the following problem, I have a form which is valid with smoke.js in the following way:

$('#btn_guardar').click(function () {
    if ($('#form_datos').smkValidate()) {
        var datos = 'nombreUno=' + $('#nombreUno').val() +
            '&nombreDos=' + $('#nombreDos').val() +
            '&apellidoUno=' + $('#apellidoUno').val() +
            '&apellidoDos=' + $('#apellidoDos').val() +
            '&personal=1';
        $.ajax({
            type: 'POST',
            url: 'formulario.php',
            data: datos
        }).done(function (data) {
            if (data == 1) {
                $.smkAlert({
                    text: 'Se actualizaron correctamente los datos',
                    type: 'success',
                });
            } else {
                $.smkAlert({
                    text: 'Se presento un problema intente de nuevo',
                    type: 'danger'
                });
            }
        });

    }
});

With the data of the form I updated a table in Mysql and after updating the data I made a new query to the database and again depending on the result it takes me to one or another page, but although it updates the data the query is skipped and throws me the smkAlert danger that is in the script when a problem occurs.

This is the query I use:

$smtr=$db->prepare('UPDATE persona SET nombreUno=:nombreUno,nombreDos=:nombreUno,apellidoUno=:apellidoUno,apellidoDos=:apellidoDos  WHERE usuario=:usuario');
            
            $smtr->bindValue('nombreUno',$_POST['nombreUno']);
            $smtr->bindValue('nombreDos',$_POST['nombreDos']);
            $smtr->bindValue('apellidoUno',$_POST['apellidoUno']);
            $smtr->bindValue('apellidoDos',$_POST['ApellidoDos']);
            $smtr->bindValue('usuario',$_SESSION['usuario']);
            $smtr->execute();
            
                   
 if($smtr){
   echo 1;
   $consulta=$db->prepare('SELECT estado FROM persona WHERE usuario=:usuario');
   $consulta->bindValue('usuario',$_SESSION['usuario']);
   $consulta->execute();
   $resultado=$consulta->fetch(PDO::FETCH_ASSOC);

      if($resultado['estado']==1){

             header('location:index.php');

        }else{
              header('location:formulario.php');
             }
 
  }else{
   echo 0;
  }  
        

What happens is that the SELECT is not executed. What changes could you implement?

    
asked by car-onte 28.12.2018 в 17:39
source

1 answer

0

I do not have much experience in PHP, but I think there is an error in your code. If I make a mistake, you correct me.

$smtr=$db->prepare('UPDATE persona SET nombreUno=:nombreUno,nombreDos=:nombreUno,apellidoUno=:apellidoUno,apellidoDos=:apellidoDos  WHERE usuario=:usuario');

$smtr->bindValue('nombreUno',$_POST['nombreUno']);

$smtr->bindValue('nombreDos',$_POST['nombreDos']);

$smtr->bindValue('apellidoUno',$_POST['apellidoUno']);

$smtr->bindValue('apellidoDos',$_POST['ApellidoDos']);

$smtr->bindValue('usuario',$_SESSION['usuario']);

$smtr->execute();

if($smtr){
  echo 1;
  $consulta=$db->prepare('SELECT estado FROM persona WHERE usuario=:usuario');

  $consulta->bindValue('usuario',$_SESSION['usuario']);

  $consulta->execute();
  
  // aqui creo que tienes el error:
  // $resultado=$consulta->fetch(PDO::FETCH_ASSOC);


  // creo que deberías ponerlo asi:
  if($consulta) {
    $resultado->fetch(PDO::FETCH_ASSOC);
    if($resultado['estado']==1){
      header('location:index.php');
    }else{
      header('location:formulario.php');
    }
  } else {
    // aqui manejas el caso que consulta es 'false'.
  } // fin if(consulta)
   
}else{
  echo 0;
}  
        

I hope this is the solution to your problem, but you will comment.

And as I said before, I do not have much experience in PHP, correct me if I'm wrong.

    
answered by 10.01.2019 в 08:20