<script>

 function save_dp() {

 $('#Upload_DP').submit(function (e) {

   e.preventDefault();


   var fileInput = $('input[name="Photo"]')[0].files[0];

   var reader = new FileReader();

   reader.onloadend = function () {

     var imageBase64 = reader.result;


     var base64Index = imageBase64.indexOf(',') + 1;

     if (base64Index !== -1) {

       imageBase64 = imageBase64.substring(base64Index);

     }


     $.ajax({

       type: 'POST',

       url: '/student_details_update_api', // Updated API endpoint URL

       data: JSON.stringify({

         imageBase64: imageBase64,

         s_id: $('input[name="s_id"]').val(),

         First_Name: $('input[name="First_Name"]').val(),

         Last_Name: $('input[name="Last_Name"]').val(),

         Gender: $('input[name="Gender"]').val(),

         DOB: $('input[name="DOB"]').val(),

         Roll: $('input[name="Roll"]').val(),

         Address: $('input[name="Address"]').val(),

         Phone: $('input[name="Phone"]').val(),

         Email: $('input[name="Email"]').val(),

         Religion: $('input[name="Religion"]').val(),

         Class: $('input[name="Class"]').val(),

         Section: $('input[name="Section"]').val(),

         Father_Name: $('input[name="Father_Name"]').val(),

         Category: $('input[name="Category"]').val(),

         Password: $('input[name="Password"]').val()

       }),

       contentType: 'application/json',

       success: function (response) {

         console.log(response);

         swal({

           title: 'Success!',

           text: 'Profile picture and data updated successfully',

           icon: 'success',

           timer: 3000,

           button: false,

           closeOnClickOutside: false,

           closeOnEsc: false

         }).then(function () {

           location.reload();

         });

       },

       error: function (error) {

         // Handle error response

       },

     });

   };


   if (fileInput) {

     reader.readAsDataURL(fileInput);

   } else {

     alert('Please select an image file');

   }

 });

}



Server side code

app.post('/student_details_update_api', (req, res) => {

 const {

   imageBase64,

   s_id,

   First_Name,

   Last_Name,

   Gender,

   DOB,

   Roll,

   Address,

   Phone,

   Email,

   Religion,

   Class,

   Section,

   Father_Name,

   Category,

   Password

 } = req.body;


 const buffer = Buffer.from(imageBase64, 'base64');

 const updateQuery = `UPDATE student SET Photo = ?, First_Name = ?, Last_Name = ?, Gender = ?, DOB = ?, Roll = ?, Address = ?, Phone = ?, Email = ?, Religion = ?, Class = ?, Section = ?, Father_Name = ?, Category = ?, Password = ? WHERE s_id = ?`;

 const values = [

   buffer,

   First_Name,

   Last_Name,

   Gender,

   DOB,

   Roll,

   Address,

   Phone,

   Email,

   Religion,

   Class,

   Section,

   Father_Name,

   Category,

   Password,

   s_id

 ];


 pool.query(updateQuery, values, (error, results) => {

   if (error) {

     console.error(error);

     res.sendStatus(500);

   } else {

     res.send(results);

   }

 });

});



http.listen(port, () => {

 console.log(`Server is running on http://localhost:${port}`);

});