I work for a company that uses this Mysqli php class to make mysql calls . The problem is that the previous programmer was not great at preventing unlimited requests. So in the whole code there are things similar to the following:
$db -> where('id',$_POST['id']);
$db -> delete('table');
This code should delete only one entry, where id = $_POST['id']. However, if $_POST['id']empty, we have problems. Then it deletes the whole table. One solution to this problem would be to find all the places in the code where the delete or update functions are called, and then make sure that the where variable is actually set.
if(isset($_POST['id']) && $_POST['id']!=''){
$db -> where('id',$_POST['id']);
$db -> delete('table');
}
But it will take a lot of work because I know that there are about 200 instances in the code. I hope there may be a way to change the following 2 functions in order to prevent them from executing unrelated requests in the first place. Any help is appreciated!
public function update($tableName, $tableData)
{
if ($this->isSubQuery)
return;
$this->_query = "UPDATE " . self::$_prefix . $tableName ." SET ";
$stmt = $this->_buildQuery (null, $tableData);
$status = $stmt->execute();
$this->reset();
$this->_stmtError = $stmt->error;
$this->count = $stmt->affected_rows;
return $status;
}
public function delete($tableName, $numRows = null)
{
if ($this->isSubQuery)
return;
$this->_query = "DELETE FROM " . self::$_prefix . $tableName;
$stmt = $this->_buildQuery($numRows);
$stmt->execute();
$this->_stmtError = $stmt->error;
$this->reset();
return ($stmt->affected_rows > 0);
}
public function where($whereProp, $whereValue = 'DBNULL', $operator = '=', $cond = 'AND')
{
if (is_array($whereValue) && ($key = key($whereValue)) != "0") {
$operator = $key;
$whereValue = $whereValue[$key];
}
if (count($this->_where) == 0) {
$cond = '';
}
$this->_where[] = array($cond, $whereProp, $operator, $whereValue);
return $this;
}
source
share