'; echo ' '; echo ' '; echo ' Weight-Track DB'; echo ' '; echo ' '; echo '

' . $s . '

'; echo ' Back to Main-Page...'; echo ' '; echo ''; } function print_error($s) { if (isset($_POST['username'])) { return print_html($s); } else { return print_json_error($s); } } include('config.php'); if ((!isset($sql_host)) || (!isset($sql_port)) || (!isset($sql_username)) || (!isset($sql_password)) || (!isset($sql_database))) { print_error('Configuration Error'); exit(1); } $conn_string = 'host=' . $sql_host . ' port=' . $sql_port . ' dbname=' . $sql_database; $conn_string = $conn_string . ' user=' . $sql_username . ' password=' . $sql_password; $db = pg_connect($conn_string); if (!$db) { print_error('Database Error: ' . pg_last_error($db)); exit(1); } $sql = 'CREATE TABLE IF NOT EXISTS weight_users ('; $sql .= 'id SERIAL,'; $sql .= 'name varchar'; $sql .= ');'; $result = pg_query($db, $sql); if (!$result) { print_error('Error (re-) creating database table for users: ' . pg_result_error($result)); exit(1); } $sql = 'CREATE TABLE IF NOT EXISTS weight_data ('; $sql .= 'id int NOT NULL,'; $sql .= 'date TIMESTAMP,'; $sql .= 'weight DECIMAL(5,2)'; $sql .= ');'; $result = pg_query($db, $sql); if (!$result) { print_error('Error (re-) creating database table for weights: ' . pg_result_error($result)); exit(1); } if (isset($_POST['username']) && isset($_POST['date']) && isset($_POST['time']) && isset($_POST['weight'])) { $sql = 'INSERT INTO weight_data(id, date, weight) VALUES (\''; $sql .= pg_escape_string(str_replace('user_', '', $_POST['username'])); $sql .= '\', '; $datetime = $_POST['date'] . $_POST['time']; $timestamp = strtotime($datetime); if (($timestamp == FALSE) || ($timestamp == -1)) { print_error('Error interpreting DateTime: "' . $datetime . '"'); exit(1); } $mysqltime = date("Y-m-d H:i:s", $timestamp); $sql .= '\'' . $mysqltime . '\', \''; $sql .= pg_escape_string($_POST['weight']) . '\')'; $result = pg_query($db, $sql); if (!$result) { print_error('Error adding new data for user "' . $_POST['username'] . '" to DB: ' . $pg_result_error($result)); } else { print_error('Added new data for user "' . $_POST['username'] . '" to DB!'); } } else if (isset($_POST['username'])) { $sql = 'INSERT INTO weight_users(name) VALUES (\''; $sql .= pg_escape_string($_POST['username']); $sql .= '\')'; $result = pg_query($db, $sql); if (!$result) { print_error('Error adding new user "' . $_POST['username'] . '" to DB: ' . pg_result_error($result)); } else { print_error('Added new user "' . $_POST['username'] . '" to DB!'); } } else if (isset($_GET['debug'])) { echo << $data_value) { if ($data_key > 0) { echo ','; } echo '{'; echo '"name": "' . $data_value['name'] . '",'; echo '"id": ' . $data_value['id'] . ','; echo '"data": ['; foreach ($data_value['data'] as $row_key => $row_value) { if ($row_key > 0) { echo ','; } echo '{'; echo '"x": "' . $row_value['date'] . '",'; echo '"y": "' . $row_value['weight'] . '"'; echo '}'; } echo ']}'; } echo ']}'; } ?>