Add to table

This commit is contained in:
Daniel Løvbrøtte Olsen 2016-05-15 02:39:40 +02:00
parent 0650a6df99
commit 83ef44f5e0

View File

@ -1,17 +1,17 @@
<?php <?php
$config = parse_ini_file('config.ini'); $config = parse_ini_file("config.ini");
$conn = mysqli_connect($config['db_host'], $config['db_user'], $config['db_password'], 'blomzt'); $conn = mysqli_connect($config["db_host"], $config["db_user"], $config["db_password"], "blomzt");
if (mysqli_connect_errno()) { if (mysqli_connect_errno()) {
die("Connection failed: " . mysqli_connect_error()); die("Connection failed: " . mysqli_connect_error());
} }
if (!TableExists($config['db_table'], $conn)) { if (!TableExists($config["db_table"], $conn)) {
$sql = 'CREATE TABLE ' . $config['db_table'] . ' ( $sql = "CREATE TABLE " . $config["db_table"] . " (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, location Point NOT NULL, url VARCHAR(65), date_added TIMESTAMP) id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, location Point NOT NULL, url VARCHAR(65), date_added TIMESTAMP)
'; ";
mysqli_query($conn, $sql); mysqli_query($conn, $sql);
} }
@ -22,9 +22,37 @@ if (!TableExists($config['db_table'], $conn)) {
<html> <html>
<head> <head>
<title>Blomzt</title> <title>Blomzt</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.1.3/material.indigo-pink.min.css">
<script defer src="https://code.getmdl.io/1.1.3/material.min.js"></script>
</head> </head>
<body> <body>
<?php
print_r($_SERVER["REQUEST_METHOD"]);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($_POST["Longtitude"] == NULL || $_POST["Latitude"] == NULL) {
echo "Please don't leave any fields blank";
exit();
}
printf("\r\n");
printf($_POST["Longtitude"]);
printf("\r\n");
printf($_POST["Latitude"]);
addToTable($_POST["Longtitude"], $_POST["Latitude"], "test", $config["db_table"], $conn);
}
?>
<form action="main.php" method="post">
<input type="text" name="Longtitude">
<input type="text" name="Latitude">
<input type="submit">
</form>
</body> </body>
</html> </html>
@ -33,8 +61,16 @@ if (!TableExists($config['db_table'], $conn)) {
// Functions // Functions
function TableExists($table, $conn) { function TableExists($table, $conn) {
$res = mysqli_query($conn, 'SHOW TABLES LIKE \'$table\''); $res = mysqli_query($conn, "SHOW TABLES LIKE '$table'");
return mysqli_num_rows($res) > 0; return mysqli_num_rows($res) > 0;
} }
function addToTable($lon, $lat, $url, $table, $conn) {
$sql = "INSERT INTO `" . $table . "` (`id`, `location`, `url`, `date_added`) VALUES (NULL, GeomFromText('POINT(" . $lon . $lat . ")',4326), 'test', CURRENT_TIMESTAMP)";
//$sql = mysqli_real_escape_string($conn, $sql);
printf($sql);
$res = mysqli_query($conn, $sql);
($res) ? printf("true") : printf("false");
}
?> ?>