0

first of all I'm new with jQuery and PHP. My aim is to create a signup form, passing data to my server on wamp. My query is working, a new record is created in the database, but I can't figure out why it is showing the error log of the ajax call instead of the success one.

Ajax call

 $('#reg-button').click(function(event){
    $.ajax({
        url: 'http://localhost:80/project/signup.php',
        type: 'POST',
        dataType: 'json',
        data: {
                    email:$("#email").val(), 
                    username:$("#username").val(), 
                    password:$("#password").val()
        },  
        error: function() {
            console.log("error");
        },
        success:function(data){
            console.log("success");
            var responseData = jQuery.parseJSON(data);
        }
    });
    event.preventDefault();
});

PHP

$response = array();
if (isset($_POST['email']) && isset($_POST['username']) && isset($_POST['password'])){

    $email = $_POST['email'];
    $username = $_POST['username'];
    $password = $_POST['password'];

    $query = "INSERT INTO user (email, username, password) VALUES ('$email', '$username', '$password')";

    $result = mysqli_query($conn, $query);

    if($result){
      $row_cnt = mysqli_num_rows($result);
      printf("Result set has %d rows.\n", $row_cnt);
      mysqli_free_result($result);
      $response['success'] = 1;
      $response['message'] = "User registered";  
      echo json_encode($response);
    } else {
      $response['success'] = 0;
      $response['message'] =  'Ops, user not registered';   
      echo json_encode($response); 
    }
} else {
  $response['success'] = 0;
  $response['message'] = 'Required fields are missing';
  echo json_encode($response);
}

Could you help me with fixing these problems, giving me some tips or tutorials?

Kwr93
  • 59
  • 1
  • 9
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Dec 16 '15 at 16:46
  • Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). – Jay Blanchard Dec 16 '15 at 16:47
  • You dont need to do `jQuery.parseJSON(data);` as you specified `dataType: 'json',` – RiggsFolly Dec 16 '15 at 16:50
  • Is the javascript receiving the output from this `printf("Result set has %d rows.\n", $row_cnt);` remember anything output will be sent back to the javascript as a responce – RiggsFolly Dec 16 '15 at 16:59
  • @JayBlanchard Thanks, I will consider it. – Kwr93 Dec 17 '15 at 10:09
  • @RiggsFolly No actually it's not showing the output from the printf – Kwr93 Dec 17 '15 at 10:10
  • So do you see the `Ops, user not registered` message? Or the `Required fields are missing` message? – RiggsFolly Dec 17 '15 at 10:11
  • i see 'Required fields are missing' even though the data are inserted in the database. – Kwr93 Dec 17 '15 at 10:25
  • Thats not possible with the code you posted! Are you sure the database gets updated? Make sure you are adding new data and not looking at a row that got created some other way. – RiggsFolly Dec 17 '15 at 10:32
  • You were right! There was a little typing mistake, now it is displaying the "User registered" message. Thanks for your help! – Kwr93 Dec 17 '15 at 11:14

0 Answers0