🐛 Fix unique
This commit is contained in:
parent
6fcba2973c
commit
50beaf9a1f
2 changed files with 12 additions and 3 deletions
|
@ -29,6 +29,13 @@ class Database {
|
||||||
return $stmt;
|
return $stmt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function checkUnique($table, $field, $value) {
|
||||||
|
$query = "SELECT * from {{table}} where {{field}} = :fieldValue";
|
||||||
|
$query = str_replace("{{field}}", $field, $query);
|
||||||
|
$stmt = $this->doquery($table, $query, ['fieldValue' => $value]);
|
||||||
|
return $stmt->fetch(PDO::FETCH_ASSOC) != null;
|
||||||
|
}
|
||||||
|
|
||||||
public function getNumQueries() {
|
public function getNumQueries() {
|
||||||
return $this->numqueries;
|
return $this->numqueries;
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,14 +57,16 @@ class Validator {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function unique($name, $array, $keys, $database) {
|
public function unique($name, $array, $keys, $database) {
|
||||||
|
global $db;
|
||||||
|
|
||||||
$value = Validator::getValue($array, $keys);
|
$value = Validator::getValue($array, $keys);
|
||||||
$table = $database["table"];
|
$table = $database["table"];
|
||||||
$dbField = $database["field"];
|
$dbField = $database["field"];
|
||||||
|
|
||||||
$query = doquery("SELECT ${dbField} FROM {{table}} WHERE ${dbField}='$value' LIMIT 1", $table);
|
if ($db->checkUnique($table, $dbField, $value)) {
|
||||||
if (mysqli_num_rows($query) > 0) {
|
return "{$value} already taken, it must be unique";
|
||||||
return "{$name} already taken, it must be unique";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue