Skip to content

Commit

Permalink
Added the new update function
Browse files Browse the repository at this point in the history
Made the promised update function
  • Loading branch information
halesyy committed Sep 28, 2016
1 parent 1b6d0d3 commit 44b6948
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
25 changes: 22 additions & 3 deletions classes/PSMQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,27 @@ public function oldupdate($table, $updates, $where = false, $binds = false, $deb
}
if ($debug) echo "<b>STATEMENT_TRACE: </b> $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. */
Expand All @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
]);

0 comments on commit 44b6948

Please sign in to comment.