Skip to content

Commit 809dfa5

Browse files
committed
Fixed update and where method glitches
1 parent 71758dd commit 809dfa5

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

MysqlDb.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class MysqlDB {
66
protected $_where = array();
77
protected $_query;
88
protected $_paramTypeList;
9+
protected $_crudType = null;
910

1011
public function __construct($host, $username, $password, $db) {
1112
$this->_mysql = new mysqli($host, $username, $password, $db) or die('There was a problem connecting to the database');
@@ -42,6 +43,7 @@ public function get($tableName, $numRows = NULL)
4243
$stmt->execute();
4344

4445
$results = $this->_dynamicBindResults($stmt);
46+
4547
return $results;
4648
}
4749

@@ -71,10 +73,8 @@ public function insert($tableName, $insertData)
7173
public function update($tableName, $tableData)
7274
{
7375
$this->_query = "UPDATE $tableName SET ";
74-
7576
$stmt = $this->_buildQuery(NULL, $tableData);
7677
$stmt->execute();
77-
7878
if ($stmt->affected_rows)
7979
return true;
8080
}
@@ -163,14 +163,15 @@ protected function _buildQuery($numRows = NULL, $tableData = false)
163163
if ($hasTableData) {
164164
$i = 1;
165165
$pos = strpos($this->_query, 'UPDATE');
166-
if ( $pos !== false) {
166+
if ( $pos !== false) {
167+
$this->_crudType = 'update';
167168
foreach ($tableData as $prop => $value) {
168169
// determines what data type the item is, for binding purposes.
169170
$this->_paramTypeList .= $this->_determineType($value);
170171

171172
// prepares the reset of the SQL query.
172173
if ($i === count($tableData)) {
173-
$this->_query .= $prop . " = ? WHERE " . $where_prop . "= " . $where_value;
174+
$this->_query .= $prop . " = ? WHERE $where_prop = '$where_value'";
174175
} else {
175176
$this->_query .= $prop . ' = ?, ';
176177
}
@@ -184,11 +185,9 @@ protected function _buildQuery($numRows = NULL, $tableData = false)
184185
$this->_query .= " WHERE " . $where_prop . "= ?";
185186
}
186187
}
187-
188188
// Determine if is INSERT query
189-
if ($hasTableData) {
189+
if ($hasTableData && !isset($this->_crudType)) {
190190
$pos = strpos($this->_query, 'INSERT');
191-
192191
if ($pos !== false) {
193192
//is insert statement
194193
$keys = array_keys($tableData);
@@ -214,12 +213,11 @@ protected function _buildQuery($numRows = NULL, $tableData = false)
214213
if (isset($numRows)) {
215214
$this->_query .= " LIMIT " . (int) $numRows;
216215
}
217-
218216
// Prepare query
219217
$stmt = $this->_prepareQuery();
220218

221219
// Bind parameters
222-
if ($hasTableData) {
220+
if ($hasTableData && $this->_crudType !== 'update') {
223221
$args = array();
224222
$args[] = $this->_paramTypeList;
225223
foreach ($tableData as $prop => $val) {
@@ -230,7 +228,8 @@ protected function _buildQuery($numRows = NULL, $tableData = false)
230228
if ($this->_where)
231229
$stmt->bind_param($this->_paramTypeList, $where_value);
232230
}
233-
231+
// Clear where method to prevent clashes with future operations;
232+
$this->_where = array();
234233
return $stmt;
235234
}
236235

0 commit comments

Comments
 (0)