Doing stuff

This commit is contained in:
Erik Thuning 2018-07-04 16:50:40 +02:00
parent f6bad2d2d9
commit f0b664d559
5 changed files with 153 additions and 1 deletions

2
.gitignore vendored

@ -1,3 +1,5 @@
*~
\#*
.#*
.htaccess
config.php

33
db_model.txt Normal file

@ -0,0 +1,33 @@
product:
id - primary auto nonull
name - nonull
invoice - nonull
location - nonull
product_info:
id - primary auto nonull
product - foreign nonull, composite uniq w field
field - nonull, composite uniq w product
data
product_tag:
id - primary auto nonull
product - foreign nonull, composite uniq w tag
tag - nonull, composite uniq w product
user:
id - primary auto nonull
name - nonull
notes
loan:
id - primary auto nonull
user - foreign nonull
product - foreign nonull
starttime - nonull
endtime - nonull
active - auto(true) nonull
system:
key - primary auto nonull
value

28
include/db.php Normal file

@ -0,0 +1,28 @@
<?php
require_once('./include/sql.php');
class Product {
private $id = 0;
private $name = '';
private $invoice = '';
private $location = '';
private $info = array();
public function getId() {
return $this->id;
}
public function getName() {
return $this->name;
}
public function setName($newname) {
$this->name = $newname;
$update = prepare('update `product` set `name` = ? where `id` = ?');
bind($update, 'si', $this->name, $this->id);
return execute($update);
}
}
?>

75
include/sql.php Normal file

@ -0,0 +1,75 @@
<?php
require_once("./config.php");
$db = new mysqli($db_host, $db_user, $db_pass, $db_name);
if($db->connect_errno) {
$error = 'Failed to connect to db. The error was: '.$db->connect_error;
throw new Exception($error);
}
function prepare($statement) {
global $db;
if(!($s = $db->prepare($statement))) {
$error = 'Failed to prepare the following statement: '.$statement;
$error .= '\n';
$error .= $db->error.' ('.$db->errno.')';
throw new Exception($error);
exit(1);
}
return $s;
}
function bind($statement, $types, ...$values) {
global $db;
return $statement->bind_param($types, ...$values);
}
function execute($statement) {
if(!$statement->execute()) {
return error('Databasfel: '.$statement->error);
}
return true;
}
function result_list($statement) {
return $statement->get_result()->fetch_all(MYSQLI_ASSOC);
}
function result_single($statement) {
$out = result_list($statement);
switch(count($out)) {
case 0:
return null;
case 1:
foreach($out as $value) {
return $value;
}
default:
throw new Exception('More than one result available.');
}
}
function begin_trans() {
global $db;
$db->begin_transaction(MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT);
}
function commit_trans() {
global $db;
$db->commit();
return true;
}
function revert_trans() {
global $db;
$db->rollback();
return false;
}
?>

@ -8,5 +8,19 @@ if(isset($_GET['lang'])) {
$lang = $_GET['lang'];
}
print format_page($lang, 'TEST', 'TESTING TITLE');
$action = 'start';
if(isset($_GET['action'])) {
$action = $_GET['action'];
}
switch($action) {
case 'start':
default:
print format_page($lang, 'TEST', 'TESTING TITLE');
break;
case 'do':
print "hej";
break;
}
?>