File path doesn't get insert in the database

I have 3 php file and 1 js file… register.php has a form which in action to signup.php, where validation is done through users.php and response is displayed on form through ajax…I don’t know why this code doesn’t insert $path to the table field call image_profile… please guide me…Thanks
register.php

<div class="modal fade my-modal" id="signup" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">

<div class="modal-dialog modal-dialog-scrollable" role="document">

        <div class="modal-content">

          <div class="modal-header">

            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>

            <h4 class="modal-title" id="myModalLabel">Signup for a new account</h4>

          </div>

          <div class="modal-body">

            <form class="form" method="post" action="signup.php" enctype="multipart/form-data">

              <div class="form-group">

                <label for="InputName">Full name</label>

                <input type="text" class="form-control" name="name" id="exampleInputName" placeholder="Name">

              </div>     

                                                         

            <div class="form-group">

            <label for="exampleInputFile">File input</label>

            <input type="file" name="image" class="form-control-file" id="exampleInputFile" aria-describedby="fileHelp">

                        

            </div>

              <button type="submit" class="btn btn-primary btn-lg">Sign me Up!</button>

            </form>

          </div>

          <div class="modal-footer">

                        

          </div>

        </div>

      </div>

    </div>

user.php

<?PHP

class user{

public function signup($user, $db){

    if(empty($user['name'])){

        return 'missing_fields';

    }else 

    {

        $valid_extentions =array('jpeg','jpg','png');

        $path= "public/images/";

        if(isset($_FILES['image']) && !empty($_FILES['image'])){    

            

           $img= $_FILES['image']['name'];

           $tmp= $_FILES['image']['tmp'];

           $ext = strlower(pathinfo($img,PATHINFO_EXTENSION));

           $final_img = rand(1000,1000000).$img;

           if(in_array($ext,$valid_extentions)){

               $path=$path.strtolower($final_img);

               move_uploaded_file($tmp,$path);

           }

                

        //`profile_image`,

        $sql = "INSERT INTO `user`(`name`,`profile_images`,`verification_code`) VALUES(?,?,?)";

        $statement = $db->prepare($sql);

            if(is_object($statement)){

                        

                $code = generateCode();

                

                $imgnewfil="testing";

                $imgnewfile = $filename;

                $statement->bindParam(1, $user['name'], PDO::PARAM_STR);

                $statement->bindParam(2, $path, PDO::PARAM_STR);

                $statement->bindParam(3, $code, PDO::PARAM_STR);

                $statement->execute();

                if($statement->rowCount()){

                    return 'success';

                }

            }        

            }

        }

    return 'error';

        }

}

$user = new User;

?>

signup.php

<?php

require_once 'includes/init.php';

$status = $user->signup($_POST, $db);

if( $status === 'success'){

    echo json_encode([

        'success'=> 'success', 

        'message'=> '<p class="alert alert-success">You are signed up successfully!</p>',

        //'url' => 'index.php',

        'signout' => 1,

        

    ]);

}else if( $status === 'missing_fields'){

    echo json_encode([

        'error'=> 'error', 

        'message'=> '<p class="alert alert-danger">All fields mandatory!</p>',

       

   ]);

}else if( $status === 'error'){

    echo json_encode([

        'error'=> 'error', 

        'message'=> '<p class="alert alert-danger">Failed to sign you up!</p>'

    ]);

}
?>

script.js

$(function(){

    

    $('.form').on('submit', function( e ){

        e.preventDefault();

        $form = $(this);

        submitForm($form);

    });

    $('#forgot-password').on('click', function( e ){

        e.preventDefault();

        $('#login').modal('hide');

    });

});

function submitForm($form){

    $footer = $form.parent('.modal-body').next('.modal-footer');

    

    $footer.html('<img src="public/images/ajax-loader.gif">');

    $.ajax({

        url: $form.attr('action'),

        method: $form.attr('method'), 

        data: $form.serialize(),

        success: function(response){

            response = $.parseJSON(response);

            

            if(response.success){

                

        //  if(!response.signout){

        //          setTimeout(function(){

        //              $footer.html( response.message );

        //              window.location = response.url;

        //          },5000);

        //      }

        //   $footer.html( response.message );

                         

        //  }

        //  else if(response.error){

        //      $footer.html( response.message );

        //  }

            console.log(response);

         }

     });

}

Someone using the same/similar code base posted a thread a few days ago with the same problem. See the reply they got - Trying to insert image path to database. However, it doesn't work

Hi, I have done the below change to the script.js file, and my form registration with the image works perfectly. However, the sign-up part has stopped working.
**script.js

$(function(){  

    $('.form').on('submit', function(e){

        e.preventDefault();

        $form = $(this);

        submitForm($form);     

    });

    $('#forgot-password').on('click', function( e ){

        e.preventDefault();

        $('#login').modal('hide');

    });

});

function submitForm($form){

    $footer = $form.parent('.modal-body').next('.modal-footer');    

    $footer.html('<img src="public/images/ajax-loader.gif">');

    var data = new FormData(this.form);

    $.ajax({

        url: $form.attr('action'),

        method: $form.attr('method'),      
        data: data,            

       processData: false,

        contentType: false,

        success: function(response){

            response = $.parseJSON(response);            

            if(response.success){                

            if(!response.signout){

                    setTimeout(function(){

                    $footer.html( response.message );

                    window.location = response.url;

                },5000);

            }

             $footer.html( response.message );                        

         }

         else if(response.error){

            $footer.html( response.message );

         }

            console.log(response)

         }

     });

}

auth.php

    <?php

    require_once 'includes/init.php';

    $status = $user->login($_POST, $db);

    if( $status === 'success'){

        echo json_encode([

            'success'=> 'success', 

            'message'=> '<p class="alert alert-success">Authenticated successfully!</p>',

            'url' => 'profile.php',

        ]);

    }

    else if( $status === 'missing_fields'){

        echo json_encode([

            'error'=> 'error', 

            'message'=> '<p class="alert alert-danger">All fields mandatory!</p>',

        ]);

    }

    else if( $status === 'error'){

        echo json_encode([

            'error'=> 'error', 

            'message'=> '<p class="alert alert-danger">Incorrect email or password!</p>'

        ]);

    }
    ?>

users.php

    <?
    class User{

        public function login($user, $db){

            if(empty($user['email']) OR empty($user['password'])){

                return 'missing_fields';

            }

            $sql = "SELECT * FROM `users` WHERE `email`=?";

            $statement = $db->prepare($sql);

            

            if( is_object($statement) ){

                $statement->bindParam(1, $user['email'], PDO::PARAM_STR);

                $statement->execute();

                if($row = $statement->fetch(PDO::FETCH_OBJ)){ // you the database and verify if the password and email match or not

                    

                    if(password_verify($user['password'], $row->password)){

                        

                        $_SESSION['logged_in'] = [

                            'id'    =>  $row->id,

                            'name'  =>  $row->name,

                        ]; 

                        return 'success';

                    }               

                }

            }

            return 'error';

        }
    }
    ?>

login.php

    <?php
    <div class="modal fade my-modal" id="login" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">

          <div class="modal-dialog" role="document">

            <div class="modal-content">

              <div class="modal-header">

                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>

                <h4 class="modal-title" id="myModalLabel">Login to your account</h4>

              </div>

              <div class="modal-body">

                <form class="form" method="POST" action="auth.php">

                  <div class="form-group">

                    <label for="exampleInputEmail1">Email address</label>

                    <input type="email" class="form-control" name="email" id="exampleInputEmail1" placeholder="Email">

                  </div>

                  <div class="form-group">

                    <label for="exampleInputPassword1">Password</label>

                    <input type="password" class="form-control" name="password" id="exampleInputPassword1" placeholder="Password">

                  </div>

                  <div class="checkbox">

                    <label>

                      <input type="checkbox"> Check me out

                    </label>

                  </div>

                  <button type="submit" class="btn btn-primary btn-lg">Log me In!</button>

                </form>

                <br>

                <p><a href="#" id="forgot-password" data-toggle="modal" data-target="#send-password-link">Forgot your password?</a></p>

              </div>

              <div class="modal-footer">

                                

              </div>

            </div>

          </div>

        </div>
    ?>
Sponsor our Newsletter | Privacy Policy | Terms of Service