title: How to make table update my otp code in database properly?
body: <p>I'm working on adding otp verification in my app. Problem is When i click register my app or rest api will generate new code but when use is exist app won't generate it again and nothing gonna update in my database. I'm not really experienced in this section, but this is what I've developed, as u can see there is a Update stmt in code which I've added when it was working without update, but after all this also didn't worked.</p>
  
  <p>Here are the things happen for creating user :</p>
  
  <p>Register=>If is not exist Create otp and add phone and otp to database => complete form => go to app.</p>
  
  <p>Register=>If it's exist check phone, update otp => go to app</p>
  
  <p>Here are the codes :</p>
  
  <p>Function : </p>
  
  <pre><code>function checkExistsUser($phone)
  {
      $stmt = $this-&gt;conn-&gt;prepare("SELECT * FROM User WHERE Phone=?");
      $stmt-&gt;bind_param("s",$phone);
      $stmt-&gt;execute();
      $stmt-&gt;store_result();
  
      if($stmt-&gt;num_rows &gt; 0) {
          $stmt-&gt;close();
          return true;
      }else{
          $stmt-&gt;close();
          return false;
      }
  }
  public function verifyOtp($otp)
  {
      $stmt = $this-&gt;conn-&gt;prepare("SELECT otp FROM User WHERE otp= ?");
      $stmt-&gt;bind_param("i",$otp);
      $stmt-&gt;execute();
      $stmt-&gt;store_result();
  
      if($stmt-&gt;num_rows &gt; 0) {
          $stmt-&gt;close();
          return true;
      }else{
          $stmt-&gt;close();
          return false;
      }
  
      public function registerNewOtp($phone,$otp)
      {
          $stmt = $this-&gt;conn-&gt;prepare("INSERT INTO User(Phone,otp) VALUES (?, ?)");
          $stmt-&gt;bind_param("si", $phone,$otp);
          $result = $stmt-&gt;execute();
  
          $stmt-&gt;close();
  
          if ($result) {
              $stmt = $this-&gt;conn-&gt;prepare("SELECT * FROM User WHERE Phone = ?");
              $stmt-&gt;bind_param("s", $phone);
              $stmt-&gt;execute();
              $stmt2 = $this-&gt;conn-&gt;prepare("UPDATE User SET otp WHERE Phone = ?");
              $stmt2-&gt;bind_param("i", $updateotp);
              $result = $stmt2-&gt;execute();
              $user = $stmt-&gt;get_result()-&gt;fetch_assoc();
              $stmt-&gt;close();
              return $user;
          } else
              return false;
      }
  </code></pre>
  
  <p>register.php</p>
  
  <pre><code>$response = array();
  if(isset($_POST['phone']))
  {
      $phone = $_POST['phone'];
      $otp = rand(100000, 999999);
  
      if($db-&gt;checkExistsUser($phone))
  {
  
      $db-&gt;updateOtp($phone, $otp);
      echo json_encode($response);
      $response["otp_created"] = "User already existed with " . $phone . " New Code IS $otp";
      sendSms($phone, $otp);
  
  
  
  }
      else
      {
          //Create new User
          $user = $db-&gt;registerNewOtp($phone,$otp);
          if($user)
          {
              $response['phone'] = $user["Phone"];
              echo json_encode($response);
              // send sms
              sendSms($phone, $otp);
          }
          else
          {
              $response["error_msg"] = "Unkown  error Occuired in registration!";
              echo json_encode($response);
          }
      }
  }
  else{
      $response["error_msg"]= "Required parameter (phone) is missing!";
      echo json_encode($response);
  }
  </code></pre>
  
  <p>UpdateOtp function :</p>
  
  <pre><code>public function updateOtp($phone, $otp)
  {
      $stmt = $this-&gt;conn-&gt;prepare("UPDATE `User` SET `otp` = ? WHERE Phone = ?") or die($this-&gt;conn-&gt;error);
      $stmt-&gt;bind_param("si", $phone,$otp);
      $result = $stmt-&gt;execute();
  
      return $result;
  }
  </code></pre>
  
  <p>Now i'm getting [] in response.</p>
  
  <p>My changes :</p>
  
  <pre><code>Functions :
  
  public function getOtpRecord($phone)
  {
      $stmt = $this-&gt;conn-&gt;prepare("SELECT otp FROM User WHERE Phone=?");
      $stmt-&gt;bind_param("s",$phone);
  
      if($stmt-&gt;execute()) {
          $user = $stmt-&gt;get_result()-&gt;fetch_assoc();
          $stmt-&gt;close();
  
          return $user;
      } else
          return NULL;
  }
  
  
  public function updateOtp($phone)
  {
      $otp = rand(100000, 999999);
      $stmt = $this-&gt;conn-&gt;prepare("UPDATE `User` SET `otp` = $otp WHERE Phone = ?") or die($this-&gt;conn-&gt;error);
      $stmt-&gt;bind_param("s", $phone);
      $result = $stmt-&gt;execute();
  
      return $result;
  }
  
  In register :
  
  if($db-&gt;checkExistsUser($phone))
  {
      if ($db-&gt;updateOtp($phone))
      {
  
          $user = $db-&gt;getOtpRecord($phone);
  
          if($user)
          {
              $otp = $user["otp"];
  
              echo json_encode($response);
              sendSms($phone, $otp);
  
          }
          else
          {
              $response["error_msg"] = "User does not exists!";
              echo json_encode($response);
          }
          $response["otp_created"] = "User already existed with " .$phone . " New Code IS $otp";
      }
  
  }
  </code></pre>
  
  <p>With my changes, database gonna update and sms will send with correct otp, but i'm getting [] Response.</p>