@@ -6,6 +6,7 @@ class MysqlDB {
6
6
protected $ _where = array ();
7
7
protected $ _query ;
8
8
protected $ _paramTypeList ;
9
+ protected $ _crudType = null ;
9
10
10
11
public function __construct ($ host , $ username , $ password , $ db ) {
11
12
$ 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)
42
43
$ stmt ->execute ();
43
44
44
45
$ results = $ this ->_dynamicBindResults ($ stmt );
46
+
45
47
return $ results ;
46
48
}
47
49
@@ -71,10 +73,8 @@ public function insert($tableName, $insertData)
71
73
public function update ($ tableName , $ tableData )
72
74
{
73
75
$ this ->_query = "UPDATE $ tableName SET " ;
74
-
75
76
$ stmt = $ this ->_buildQuery (NULL , $ tableData );
76
77
$ stmt ->execute ();
77
-
78
78
if ($ stmt ->affected_rows )
79
79
return true ;
80
80
}
@@ -163,14 +163,15 @@ protected function _buildQuery($numRows = NULL, $tableData = false)
163
163
if ($ hasTableData ) {
164
164
$ i = 1 ;
165
165
$ pos = strpos ($ this ->_query , 'UPDATE ' );
166
- if ( $ pos !== false ) {
166
+ if ( $ pos !== false ) {
167
+ $ this ->_crudType = 'update ' ;
167
168
foreach ($ tableData as $ prop => $ value ) {
168
169
// determines what data type the item is, for binding purposes.
169
170
$ this ->_paramTypeList .= $ this ->_determineType ($ value );
170
171
171
172
// prepares the reset of the SQL query.
172
173
if ($ i === count ($ tableData )) {
173
- $ this ->_query .= $ prop . " = ? WHERE " . $ where_prop . " = " . $ where_value ;
174
+ $ this ->_query .= $ prop . " = ? WHERE $ where_prop = ' $ where_value' " ;
174
175
} else {
175
176
$ this ->_query .= $ prop . ' = ?, ' ;
176
177
}
@@ -184,11 +185,9 @@ protected function _buildQuery($numRows = NULL, $tableData = false)
184
185
$ this ->_query .= " WHERE " . $ where_prop . "= ? " ;
185
186
}
186
187
}
187
-
188
188
// Determine if is INSERT query
189
- if ($ hasTableData ) {
189
+ if ($ hasTableData && ! isset ( $ this -> _crudType ) ) {
190
190
$ pos = strpos ($ this ->_query , 'INSERT ' );
191
-
192
191
if ($ pos !== false ) {
193
192
//is insert statement
194
193
$ keys = array_keys ($ tableData );
@@ -214,12 +213,11 @@ protected function _buildQuery($numRows = NULL, $tableData = false)
214
213
if (isset ($ numRows )) {
215
214
$ this ->_query .= " LIMIT " . (int ) $ numRows ;
216
215
}
217
-
218
216
// Prepare query
219
217
$ stmt = $ this ->_prepareQuery ();
220
218
221
219
// Bind parameters
222
- if ($ hasTableData ) {
220
+ if ($ hasTableData && $ this -> _crudType !== ' update ' ) {
223
221
$ args = array ();
224
222
$ args [] = $ this ->_paramTypeList ;
225
223
foreach ($ tableData as $ prop => $ val ) {
@@ -230,7 +228,8 @@ protected function _buildQuery($numRows = NULL, $tableData = false)
230
228
if ($ this ->_where )
231
229
$ stmt ->bind_param ($ this ->_paramTypeList , $ where_value );
232
230
}
233
-
231
+ // Clear where method to prevent clashes with future operations;
232
+ $ this ->_where = array ();
234
233
return $ stmt ;
235
234
}
236
235
0 commit comments