Ajax calls PHP file but some datas doesn't return

Viewed 40

"undone" OR "error" RETURNS. BUT "done" DOESN'T RETURN. When mail adress exists 'undone' returns, inserting new user to database works but 'done' doesn't return. I wanna see "undone" from $data["status"] return when user is inserted. How can i fix?

js

$(document).ready(function(){
   $("#form").submit(function(){
     $.ajax({
       url: "file.php",
       type: "POST",
       dataType: "json",
       data: $('#form').serialize(),
       success: function(data){
         alert(data.status);
       }
     })
   })
 })

file.php

<?php
   session_start();
   if (isset($_POST['submit'])){

     include 'config.php';
     $data = array();
     $name = $conn -> real_escape_string($_POST['username']);
     $mail = $conn -> real_escape_string($_POST['mail']);
     $psw = $conn -> real_escape_string($_POST['password']);
     $code = $conn -> real_escape_string(uniqid());
 
 
 
     $qry = $conn->query("SELECT * FROM member WHERE mail='{$mail}'");
     if (mysqli_num_rows($qry)>0) {
       $data["status"] = "undone";
     }else {
       $qry2 = $conn->prepare("INSERT INTO member (name, mail, psw, code) VALUES ('{$name}','{$mail}','{$psw}','{$code}')");
       if($qry2){
         $qry2->execute();
         $_SESSION['SESSION_MAILADDRESS'] = $mail;
         $data["status"] = "done";
 
         mysqli_free_result($qry2);
       }else {
         $data["status"] = "error";
       }
     }
     echo json_encode($data);
     mysqli_free_result($qry);
     $conn->close();
   }
  ?>

    
 
1 Answers

You need to remove

mysqli_free_result($qry2);

from your code.

As per https://php.net/manual/en/mysqli-result.free.php you're supposed to pass in a mysqli result object to that function, but now I look closely, you passed it a mysqli statement object. So that should cause a crash. I expect your AJAX call returned a 500 (internal server error) status and an error message which ought to be visible from the browser's Network tool as I described.

In any case using the mysqli_free_result command makes no sense in this context, because you don't have a result to free - a INSERT query does not generate a result set.

Related