/** * 执行修改,必须指定where才能执行。 * @param $data array 需要更新的数据 * @param $where array 条件,关联数组 */ public function update($data, $where = null) { if (isset($where)) { $this->where($where); } else { if (empty($this->where)) { print "DBModel.php: NO update data without where, if you want to do this, pls use updateAll."; exit; return null; } } $arr = array(); foreach ($data as $key => $value) { //注意 这里没做安全过滤哦 if (is_int($key)) { DBTool::conditions_push($arr, $key, $value, $this->t1); } else { if (strpos($key, ' ') !== false) { DBTool::conditions_push($arr, $key, $value, $this->t1); } else { if ($value === null) { continue; } else { // 做字段安全过滤 if (!in_array($key, $this->getMeta())) { continue; } $arr[] = sprintf('%s = \'%s\'', $key, DBTool::wrap2Sql($value)); } } } } if (count($arr) > 0) { $str = join(' , ', $arr); $sql = 'update ' . $this->tableName . ' ' . $this->t1 . ' set ' . $str . $this->where . $this->limit; return DBTool::executeSql($sql); } else { return false; } }