From 44b6948935d0344bb7ecdc6dd42b1294251ef25f Mon Sep 17 00:00:00 2001 From: Jek Hales Date: Wed, 28 Sep 2016 00:34:15 -0700 Subject: [PATCH] Added the new update function Made the promised update function --- classes/PSMQuery.php | 25 ++++++++++++++++++++++--- index.php | 7 +++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/classes/PSMQuery.php b/classes/PSMQuery.php index e2f2fb2..80394f7 100644 --- a/classes/PSMQuery.php +++ b/classes/PSMQuery.php @@ -195,8 +195,27 @@ public function oldupdate($table, $updates, $where = false, $binds = false, $deb } if ($debug) echo "STATEMENT_TRACE: $statement"; } - public function update() { - + /* The new update function - Will generate your update query and auto-bind it for you. + The where part of the statement also generates a bind for you - just do "id = 1" and itll bind. */ + public function update($table, $updates, $where = false, $debug = false) { + # Starting the query. + $statement = "UPDATE $table SET "; + # Setting the cols / vals. + $cols = array_keys($updates); + $vals = array_values($updates); + # Adds " = ?" too each of the cols values. + foreach ($cols as $index => $col) { $cols[$index] = "$col = ?"; } + $statement .= implode(', ', $cols); + if ($where) { + # Gets each side of the where statement given. + $where_parts = explode(' = ',$where); + $statement .= " WHERE {$where_parts[0]} = ?"; + array_push($vals, $where_parts[1]); + } + # Performing the query. + $query = $this->handler->prepare($statement); + $query->execute($vals); + if ($debug) { echo $statement; $this->display($vals); } } /* Returns each column entry from the table asked, so if input = Table:users, col:username - An array is returned with each username. */ @@ -207,7 +226,7 @@ public function getall($table, $col) { array_push($store, $row[$col]); } return $store; - } + } public function ga($table, $col) { $this->getall($table, $col); } /* Will go into the table and find the column, and find the ID of given ID, and increase it by how it wants. */ public function plus($table, $col, $id, $by) { diff --git a/index.php b/index.php index 8391de0..9b9beee 100644 --- a/index.php +++ b/index.php @@ -6,3 +6,10 @@ $psm = new PSM('localhost test root EM',[ 'safeconnection' => true ]); + + $psm->update('users',[ + 'username' => 'jek_fucking_loves_salami', + 'password' => 'FAMAMFMAMFMA', + 'time' => time(), + 'ip' => $_SERVER['REMOTE_ADDR'] + ]);