Sending verification code

rsbypi4

New member
I am beginner to make a simple page using php.
Lets assume, I click confirm button to check the verification code.


Reset_password.php

PHP:
<?php
  session_start();
  include('config.php');
  include('gen_code.php');

  if(isset($_POST['confirm']))
  {

    $code=$_POST['code'];

    $stmt3=$mysqli->prepare("SELECT code FROM tbl_user WHERE code=? ");
    $stmt3->bind_param('s',$code);
    $stmt3->execute();
    $stmt3 -> bind_result($code);
    $res=$stmt3->fetch();
 

    if($res)
    {
   
      $succ = "success";
    }
    else
    {
      $err = "error";
    } 
  }
 
?>




After I click Confirm button they call gen_code.php to generate the verification code.

gen_code.php

PHP:
<?php
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;

    $u_id = $_GET['u_id'];
    $query="UPDATE tbl_user SET code=? WHERE u_id=?";
    $stmt1 = $mysqli->prepare($query);
    $rc=$stmt1->bind_param('si',  $code, $u_id);
    $u_auth_code = substr(number_format(time() * rand(), 0, '', ''), 0, 6);
    $stmt1->execute();
 

  $stmt2=$mysqli->prepare("SELECT u_id, u_email FROM tbl_user");
  $stmt2->execute();
  $stmt2 -> bind_result($u_id,$u_email);
  $rs=$stmt2->fetch();
 

  $_SESSION['u_id']=$u_id;
  $_SESSION['u_email']=$u_email;

  try
  {
      require './vendor/mail-src/Exception.php';
      require './vendor/mail-src/PHPMailer.php';
      require './vendor/mail-src/SMTP.php';

      $mail = new PHPMailer(true);
      $mail -> isSMTP();
      $mail -> Host = 'smtp.gmail.com';
      $mail -> SMTPAuth = true;
      //sender info
      $mail -> Username = 'sample@gmail.com';
      $mail -> Password = 'pass1234';//app pass
      $mail -> SMTPSecure = 'ssl';
      $mail -> Port = 465;

      $mail -> setFrom('sample@gmail.com');//from
      $mail -> addAddress($u_email);//customer address
      $mail -> isHTML(true);

      $mail -> Subject = ('AutoConnect verification code');
      $mail-> Body = '<p>Your verification code is: <b style="font-size: 30px;">' . $code .'</b></p>';
      $mail ->send();
    
 

  }
  catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
  }
?>



My problem is, during I enter my correct verification code from my email it show some error said, " Fatal error: Uncaught mysqli_sql_exception: Commands out of sync; you can't run this command now in C:\xampp\htdocs\.... "

Can someone help me to fix my problem, Thank you so much in advance.
 
Last edited:
I am beginner to make a simple page using php.
Lets assume, I click confirm button to check the verification code.


Reset_password.php

PHP:
<?php
  session_start();
  include('config.php');
  include('gen_code.php');

  if(isset($_POST['confirm']))
  {

    $code=$_POST['code'];

    $stmt3=$mysqli->prepare("SELECT code FROM tbl_user WHERE code=? ");
    $stmt3->bind_param('s',$code);
    $stmt3->execute();
    $stmt3 -> bind_result($code);
    $res=$stmt3->fetch();
 

    if($res)
    {
  
      $succ = "success";
    }
    else
    {
      $err = "error";
    }
  }
 
?>




After I click Confirm button they call gen_code.php to generate the verification code.

gen_code.php

PHP:
<?php
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;

    $u_id = $_GET['u_id'];
    $query="UPDATE tbl_user SET code=? WHERE u_id=?";
    $stmt1 = $mysqli->prepare($query);
    $rc=$stmt1->bind_param('si',  $code, $u_id);
    $u_auth_code = substr(number_format(time() * rand(), 0, '', ''), 0, 6);
    $stmt1->execute();
 

  $stmt2=$mysqli->prepare("SELECT u_id, u_email FROM tbl_user");
  $stmt2->execute();
  $stmt2 -> bind_result($u_id,$u_email);
  $rs=$stmt2->fetch();
 

  $_SESSION['u_id']=$u_id;
  $_SESSION['u_email']=$u_email;

  try
  {
      require './vendor/mail-src/Exception.php';
      require './vendor/mail-src/PHPMailer.php';
      require './vendor/mail-src/SMTP.php';

      $mail = new PHPMailer(true);
      $mail -> isSMTP();
      $mail -> Host = 'smtp.gmail.com';
      $mail -> SMTPAuth = true;
      //sender info
      $mail -> Username = 'sample@gmail.com';
      $mail -> Password = 'pass1234';//app pass
      $mail -> SMTPSecure = 'ssl';
      $mail -> Port = 465;

      $mail -> setFrom('sample@gmail.com');//from
      $mail -> addAddress($u_email);//customer address
      $mail -> isHTML(true);

      $mail -> Subject = ('AutoConnect verification code');
      $mail-> Body = '<p>Your verification code is: <b style="font-size: 30px;">' . $code .'</b></p>';
      $mail ->send();
   
 

  }
  catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
  }
?>



My problem is, during I enter my correct verification code from my email it show some error said, " Fatal error: Uncaught mysqli_sql_exception: Commands out of sync; you can't run this command now in C:\xampp\htdocs\.... "

Can someone help me to fix my problem, Thank you so much in advance.
After executing the first prepared statement ($stmt1), the code attempts to execute a second prepared statement ($stmt2) without properly closing or freeing the result set from the first statement. In MySQLi, when using prepared statements, you must ensure that all results are fetched or the statement is closed before executing another statement on the same connection.
To resolve the issue, you need to ensure that the result set from the first statement is fully processed before executing the second statement.
 
After executing the first prepared statement ($stmt1), the code attempts to execute a second prepared statement ($stmt2) without properly closing or freeing the result set from the first statement. In MySQLi, when using prepared statements, you must ensure that all results are fetched or the statement is closed before executing another statement on the same connection.
To resolve the issue, you need to ensure that the result set from the first statement is fully processed before executing the second statement.
I already fix this issue, I close first $stmt1 by using $stmt1->close(); . Thank you so much Moorcam.
 
Back
Top