Summary Files Commits Branches @commit @diff @stats
path: root / src / models / DB.php
diff options
context:
space:
mode:
1 files changed, 8 insertions, 0 deletions
-rw-r--r--src/models/DB.php8
diff --git a/src/models/DB.php b/src/models/DB.php
index c14b9d3..f424334 100644
--- a/src/models/DB.php
+++ b/src/models/DB.php
@@ -34,6 +34,8 @@ class DB {
}
/**
+ * Generates a foreign key string from the given parameters.
+ *
* @return string
*/
static function genFK(string $currentColumn, string $foreignTable, string $foreignColumn) {
@@ -59,10 +61,12 @@ class DB {
$query = "CREATE TABLE IF NOT EXISTS {$tableName}(
id INT NOT NULL AUTO_INCREMENT, ";
+ // Create column parts of query string
foreach ($columns as $column) {
$query .= "{$column}, ";
}
+ // Create key(s) parts of query string
$query .= 'PRIMARY KEY (id), ';
foreach ($foreignKeys as $fk) {
@@ -91,7 +95,9 @@ class DB {
function insertValue(string $tableName, ...$values) {
$query = "INSERT INTO {$tableName} VALUES(NULL, ";
+ // Create the values part of the query string
foreach($values as $value) {
+ // In queries, strings must be surrounded in single quotes
if (gettype($value) == 'string')
$query .= "'{$value}', ";
else
@@ -143,9 +149,11 @@ class DB {
function updateById(string $tableName, int $id, ...$values) {
$query = "UPDATE {$tableName} SET ";
+ // Create the name = value part of query string
for ($i = 0; $i < sizeof($values); $i += 2) {
$query .= "{$values[$i]} = ";
+ // In queries, strings must be surrounded in single quotes
if (gettype($values[$i+1]) == 'string')
$query .= "'{$values[$i+1]}'";
else