🐛 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;
|
||||
}
|
||||
|
||||
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() {
|
||||
return $this->numqueries;
|
||||
}
|
||||
|
|
|
@ -57,14 +57,16 @@ class Validator {
|
|||
}
|
||||
|
||||
public function unique($name, $array, $keys, $database) {
|
||||
global $db;
|
||||
|
||||
$value = Validator::getValue($array, $keys);
|
||||
$table = $database["table"];
|
||||
$dbField = $database["field"];
|
||||
|
||||
$query = doquery("SELECT ${dbField} FROM {{table}} WHERE ${dbField}='$value' LIMIT 1", $table);
|
||||
if (mysqli_num_rows($query) > 0) {
|
||||
return "{$name} already taken, it must be unique";
|
||||
if ($db->checkUnique($table, $dbField, $value)) {
|
||||
return "{$value} already taken, it must be unique";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue