From 3831fa1ae9933a5e2e3fc76ca62b3d150b1640bf Mon Sep 17 00:00:00 2001 From: Dzejkobik007 Date: Mon, 20 Mar 2023 13:24:37 +0100 Subject: [PATCH] Update --- wap/php-sqlcont/Mysql.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/wap/php-sqlcont/Mysql.php b/wap/php-sqlcont/Mysql.php index 8ff02e2..db20a2a 100644 --- a/wap/php-sqlcont/Mysql.php +++ b/wap/php-sqlcont/Mysql.php @@ -29,12 +29,18 @@ class MySQL implements IDB return []; } - function insert(string $table, array $data): bool { - $query = "INSERT INTO `".$table."` VALUES ("; - for($i = 0; $i < sizeof($data); $i++) { - $query .= "'".$data[$i]."', "; + function insert(string $table, array $data): bool { + $keys = "("; + $values = "("; + foreach(array_keys($data) as $key) { + $keys .= "'".$key."', "; } - $query .= ")"; + foreach($data as $row) { + $values .= "'".$row."', "; + } + $keys .= ")"; + $values .= ")"; + $query = "INSERT INTO `".$table."` ".$keys." VALUES ".$values; $result = mysqli_query($this->db, $query); return $result ? true : false; }