farfnd

update.php

Dec 14th, 2021
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.88 KB | None | 0 0
  1. <?php
  2.  
  3. include("config.php");
  4.  
  5. header("Access-Control-Allow-Origin: *");
  6. header("Access-Control-Allow-Headers: *");
  7. header("Access-Control-Allow-Methods: *");
  8.  
  9. if($_SERVER['REQUEST_METHOD'] == 'POST'){
  10.     $id = str_replace("'", "\'", $_POST['id']);
  11.     $nama = str_replace("'", "\'", $_POST['nama']);
  12.     $jenis_kelamin = str_replace("'", "\'", $_POST['jenis_kelamin']);
  13.     $agama = str_replace("'", "\'", $_POST['agama']);
  14.     $sekolah_asal = str_replace("'", "\'", $_POST['sekolah_asal']);
  15.     $alamat = str_replace("'", "\'", $_POST['alamat']);
  16.     $foto = "";
  17.    
  18.     if(!empty($_FILES['foto']['name'])){
  19.         if($_FILES['foto']['size'] > 3*1048576) { //3 MB (size is also in bytes)
  20.             die(json_encode([
  21.                 "error" => 500,
  22.                 "status" => "File too large (> 3 MB)"
  23.             ]));
  24.             exit;
  25.         }
  26.        
  27.         $filename = $_FILES['foto']['name'];
  28.        
  29.         $location = "../images/".$filename;
  30.         $imageFileType = pathinfo($location,PATHINFO_EXTENSION);
  31.         $imageFileType = strtolower($imageFileType);
  32.         $imageNewFileName = md5(time()).'.'.$imageFileType;
  33.         $location = "../images/".$imageNewFileName;
  34.  
  35.         $valid_extensions = array("jpg","jpeg","png");
  36.      
  37.         $response = 0;
  38.         /* Check file extension */
  39.         if(in_array(strtolower($imageFileType), $valid_extensions)) {
  40.            /* Upload file */
  41.            if(move_uploaded_file($_FILES['foto']['tmp_name'],$location)){
  42.               $response = $location;
  43.               $foto = $imageNewFileName;
  44.            }
  45.         }else{
  46.             die(json_encode([
  47.                 "error" => 500,
  48.                 "status" => "Invalid file type"
  49.             ]));
  50.             exit;
  51.         }  
  52.        
  53.         $sql = "SELECT * FROM calon_siswa WHERE id=$id";
  54.         $query = mysqli_query($db, $sql);
  55.  
  56.         $old_foto = "";
  57.         while ($siswa = mysqli_fetch_array($query)) {
  58.             $old_foto = $siswa["foto"];
  59.             break;
  60.         }
  61.        
  62.         if($old_foto && file_exists($old_foto)){
  63.             unlink($old_foto);
  64.         }
  65.  
  66.         $sql = "UPDATE calon_siswa
  67.                SET nama='$nama', jenis_kelamin='$jenis_kelamin', agama='$agama', sekolah_asal='$sekolah_asal', alamat='$alamat', foto='$foto'
  68.                WHERE id=$id";
  69.     } else {
  70.         $sql = "UPDATE calon_siswa
  71.                SET nama='$nama', jenis_kelamin='$jenis_kelamin', agama='$agama', sekolah_asal='$sekolah_asal', alamat='$alamat'
  72.                WHERE id=$id";
  73.     }
  74.  
  75.     $query = mysqli_query($db, $sql);
  76.    
  77.     if ($query) {
  78.         die(json_encode([
  79.             "error" => 0,
  80.             "status" => "OK"
  81.         ]));
  82.     } else {
  83.         die(json_encode([
  84.             "error" => 500,
  85.             "status" => "Internal Server Error"
  86.         ]));
  87.     }
  88. }else{
  89.     die("Method not allowed");
  90. }
Advertisement
Add Comment
Please, Sign In to add comment