Public 函数,在将数据添加到 table 之前检查两列是否重复

Public function, checking for duplicate in two columns before adding data to the table

我的 table 中有如下三列:

product_id
sku
model

我正在尝试编写一个函数,每次我向列 model 添加一个新值时,它都会检查以确保该值在 model 和 [=16= 中是唯一的] 提供了独特的 product_id。 我现在有这个功能,可以在让 model 列添加值之前检查现有的 model。但我还需要它在 model 列添加新值时检查 sku 以确保新值当前既不存在于 model 列也不存在于 sku列。

public function checkmodel($model, $product_id=0) {
  if (!$model) {return false; }
    $sql = "SELECT * FROM `".DB_PREFIX."product` WHERE model='".$this->db->escape($model)."' AND sku='" AND product_id<>'".(int)$product_id."'";
    $query = $this->db->query($sql);
    return $query->rows;

我必须删除 $model$sku 值的转义符以使其更易于阅读;把那些放回去。

    public function checkmodel($model, $product_id=0) {
      if (!$model) {return false; }
      $sql = "SELECT COUNT(*) AS total FROM `".DB_PREFIX."product` WHERE
  (model='". $model."' OR sku='" . $model . "') 
  AND product_id != '".(int)$product_id."'";
      $query = $this->db->query($sql);
      if ($query->row['total'] != 0) return false; 
    }