Error sending data with prepare and mysqli

0

I am trying to send information to my database after making two queries, the first if the user already exists and the second if the email already exists subsequently made the "upload of information" to my database with my sentence prepared This is my code:

             $loadtodatabase = $conn_reg->prepare("INSERT INTO usus (id, paterno ,materno ,nombre ,usuario ,pass ,email ,lvl ,telefono ,fecha ,code ,active ,profileimg)VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) ");
             $loadtodatabase->bind_param("sssssssssssss", $id, $paterno,  $materno,  $nombre,  $usuario,  $pass,  $email,  $lvl,  $telefono,  $joining_date,  $code,  $active,  $profileimg );

             $checkuser = $conn_reg->prepare ("SELECT 'id', 'nombre', 'paterno', 'materno', 'usuario', 'pass', 'email', 'lvl', 'telefono', 'fecha', 'code', 'active', 'profileimg' FROM 'usus' WHERE 'usuario' =  ?"); 
             $checkuser->bind_param("s", $nombre);
             $checkuser->execute();
             $checkuser->bind_result($id_d,$nombre_d,$paterno_d,$materno_d,$usuario_d,$pass_d,$email_d,$lvl_d,$telefono_d,$fecha_d,$code_d,$active_d,$profileimg_d);
            if ($checkuser->fetch()) {
                  if($usuario = $usuario_d) { 
                      echo "ya fue utilizado el nombre";


                  }else { 
                       $checkmail = $conn_reg->prepare ("SELECT 'id', 'nombre', 'paterno', 'materno', 'usuario', 'pass', 'email', 'lvl', 'telefono', 'fecha', 'code', 'active', 'profileimg' FROM 'usus' WHERE 'email' =  ?"); 
                       $checkmail->bind_param("s", $email);
                          $checkmail->execute();
                          $checkmail->bind_result($id_d,$nombre_d,$paterno_d,$materno_d,$usuario_d,$pass_d,$email_d,$lvl_d,$telefono_d,$fecha_d,$code_d,$active_d,$profileimg_d);
                          if ($checkmail->fetch()) {
                          if($email = $email_d) { 
                              echo "ya fue utilizado el correo";
                           }else {
                           if( $loadtodatabase->execute()){
                           echo "datos guardados"; 
                           }else {echo "error ";
                              }                            
                           }
                        }                                   
                     }
                  }

if I use already registered users, if you give me the errors, depending if it is a user already busy or an email already occupied, but at the end of everything does not save the information in the database.

In case you do not understand the code very much, I explain it quickly:

Variables are loaded from $ _POST (this is not seen in the code, do not consider it useful)

The prepared statement is made to write the information but it is not executed.

queries if the variable $ user exists in the database (if it exists, it shows the error if it does not exist, it continues with an "else")

queries if the variable $ email exists in the database (if it exists, it shows the error if it does not exist, it continues with an "else")

------- Here is the problem ---------- if $ loadtodatabase manages to run it shows "saved data" (otherwise it shows error).

How can I solve that fault? the variables are well written and if I do "echo" to see if they have saved data shows them.

    
asked by Kevin Mora 03.04.2018 в 04:12
source

2 answers

1

You can try something similar to this:

$checkuser = $conn_reg->prepare("SELECT id FROM usus WHERE usuario = ?"); 
$checkuser->bind_param("s", $nombre);
$checkuser->execute();

$checkuser->store_result();
$num_of_rows = $checkuser->num_rows;

if($num_of_rows > 0){
    echo "ya fue utilizado el nombre";
}else{

    $checkMail = $conn_reg->prepare("SELECT id FROM usus WHERE email = ?"); 
    $checkMail->bind_param("s", $email);
    $checkMail->execute();

    $checkMail->store_result();
    $num_of_rows2 = $checkMail->num_rows;

    if($num_of_rows2 > 0){
        echo "ya fue utilizado el correo";
    }else{

        $insert_q = "INSERT INTO usus (id, paterno,materno ,nombre ,usuario ,pass ,email ,lvl ,telefono ,fecha ,code ,active ,profileimg)VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) ";
        $loadtodatabase = $conn_reg->prepare($insert_q);
        $loadtodatabase->bind_param("sssssssssssss", $id, $paterno,  $materno,  $nombre,  $usuario,  $pass,  $email,  $lvl,  $telefono,  $joining_date,  $code,  $active,  $profileimg);

        if($loadtodatabase->execute()){
            echo "INSERTADO!";
        }else{

            printf("Error: %s.\n", $loadtodatabase->error);
        }
    }
} 

I particularly use PDO, I hope I can help you anyway.

    
answered by 03.04.2018 в 04:26
0

Well, I solved it! use printf("Error: %s.\n", $loadtodatabase->error); to see what was happening and it gave me as a result that $ user or $ email could be null (so I have it in my table), and arrange the script to be executed in a more orderly way so

    $checkuser = $conn_reg->prepare ("SELECT 'id', 'nombre', 
    'paterno','materno', 'usuario', 'pass', 'email', 'lvl', 
    'telefono', 'fecha', 
    'code', 'active', 'profileimg' FROM 'usus' WHERE 'usuario' =  ?"); 
     $checkuser->bind_param("s", $usuario);
     $checkuser->execute();
     $checkuser->bind_result(
     $id_d,
     $nombre_d,
     $paterno_d,
     $materno_d,
     $usuario_d,
     $pass_d,
     $email_d,
     $lvl_d,
     $telefono_d,
     $fecha_d,
     $code_d,
     $active_d,
     $profileimg_d);
     if ($checkuser->fetch()) {}
     if($usuario = $usuario_d) {
           echo "ya esta registrado el usuario";
          $checkuser->close();}else { $ok1=1;$checkuser->close();}   
     $checkmail = $conn_reg->prepare ("SELECT 'id', 'nombre',
      'paterno', 'materno', 'usuario', 'pass', 'email', 
     'lvl','telefono', 
     'fecha', 'code', 'active', 'profileimg' FROM 'usus' WHERE 'email'
      =  ?"); 
     $checkmail->bind_param("s", $email);
     $checkmail->execute();
     $checkmail->bind_result(
     $id_d,
     $nombre_d,
     $paterno_d,
     $materno_d,
     $usuario_d,
     $pass_d,
     $email_d,
     $lvl_d,
     $telefono_d,
     $fecha_d,
     $code_d,
     $active_d,
     $profileimg_d);
     if ($checkmail->fetch()) {}
     if($email = $email_d) { 
             echo "ya existe el correo";
             $checkmail->close();                        
                 }else { $ok2=1;$checkmail->close();}

                if($ok1=1) {
                if($ok2=1) {
                      /*     IMPORTANTE!!!!!!
                        reasignamos los valores que
                        se perdieron en las consultas  */
                        $usuario = $_POST['usuario'];
                        $email= $_POST['email'];            

                     $consulta=("INSERT INTO 'usus' (id,nombre, paterno, materno, usuario, pass, email, lvl, telefono, fecha, code, active, profileimg)
                     VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) ");
                if($loadtodatabase = $conn_reg->prepare($consulta)) {
                   $loadtodatabase->bind_param("issssssssssss",$id, $nombre, $paterno,$materno,$usuario,$pass,$email,$lvl,$telefono,$joining_date,$code,$active,$profileimg);
                if($loadtodatabase->execute()){echo "Ejecutado con exito!";$loadtodatabase->close();
                printf("Error: %s.\n", $loadtodatabase->error);
                }else { echo "No ejecutado"; $loadtodatabase->close();
                printf("Error: %s.\n", $loadtodatabase->error);}
                }else {echo "error en preparacion de consulta<br>";
                $loadtodatabase->close();}
                }else {echo"<h1>check value 2 fail</h1>";$loadtodatabase->close();}
                }else {echo"<h1>check value 1 fail</h1>";$loadtodatabase->close();}
    
answered by 04.04.2018 в 22:28