JSON Server 앱의 소스 코드(PHP로 작성)

11854 단어 phpjsonserverjson

데이터베이스 구조



데이터베이스: json표: jsonData필드: id, kunci, json

PHP 코드



할라만/beranda.php

<!DOCTYPE html>
<html>
<head>
    <title>JSON</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="JSON Server">
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.min.css">
    <style type="text/css">
        h3 {
            text-align: center;
        }
        .tombolTambah {
            margin: auto;
        }
    </style>
</head>
<body>
    <h3>JSON Server</h3>
    <form method="post" action="olah">
        <input type="hidden" value="<?= md5(rand()) ?>" name="kunci">
        <textarea name="json" required></textarea>
        <input type="submit" class="tombolTambah" value="Tambahkan" name="">
    </form>
    <script type="text/javascript">
        tinggi = () => document.querySelector('textarea').style.height = `${window.innerHeight - 130}px`
        tinggi()
        window.addEventListener('resize', tinggi)
    </script>
</body>
</html>


할라만/olah.php

<?php 
$db->prepare('insert into jsonData (kunci, json) values (:kunci, :json)')->execute([
    ':kunci' => $_POST['kunci'],
    ':json' => $_POST['json']
]);
header('Location: ' . $_POST['kunci']);


할라만/tampil.php

<?php 
$data = $db->query('select json from jsonData where kunci = "' . $routes[1] . '"');
$hasil = '';
while ($row = $data->fetch()) {
    $hasil = $row['json'];
}
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST");
header('Content-Type: application/json');
echo $hasil;


할라만/ubah.php

<?php 
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST");
if (!empty($_POST['json'])) {
    $db->prepare('update jsonData set json = :json where kunci = :kunci')->execute([
        ':json' => $_POST['json'],
        ':kunci' => $routes[1]
    ]);
}


.ht액세스

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]


index.php

<?php 
$db = new PDO('mysql:host=localhost;dbname=json', 'root', 'kucing');
$routes = explode('/', $_SERVER['PATH_INFO']);
if ($routes[1] == '') {
    include 'halaman/beranda.php';
} elseif ($routes[1] == 'olah') {
    include 'halaman/olah.php';
} elseif (strlen($routes[1]) > 0 && $routes[2] == 'ubah') {
    include 'halaman/ubah.php';
} elseif (strlen($routes[1]) > 0) {
    include 'halaman/tampil.php';
}

좋은 웹페이지 즐겨찾기