0
Well, I’m trying to update and check if what I’m updating already exists, I can check if it exists, but there’s a problem, as I’m updating a data, it will always exist, if I take the function that checks if it exists, the user can update the record and end up leaving 2 equal data. Have some correct way that allows you to check the data in the database and update?
Function to check if it already exists
public function existsFunction($name){
try{
$command = ("SELECT * FROM function
WHERE function_nm = '$name'");
$num_rows = $this->mysqli->query($command)->num_rows;
if($num_rows < 1){
return false;
}else{
return true;
}
}catch(Exception $err){
echo 'Erro: ', $err->getMessage();
}
}
Function to update
public function editFunction($id, $name, $access, $comment){
try{
if($this->existsFunction($name)){
return "already_exists";
}else{
$command = ("UPDATE function
SET function_nm = '$name',
access_cd = '$access',
function_ds = '$comment'
WHERE function_id = '$id'");
$query = $this->mysqli->query($command);
if(!$query){
return "error";
}else{
return "success";
}
}
}catch(Exception $err){
echo 'Erro: ', $err->getMessage();
}
}
The statement
UPDATE
does not insert anything in the database, only updates an existing record, so there is no possibility of ending up with 2 equal data. Who puts record in the database is the INSERT declaration. The way you did if it exists will never be updated. You have to reverseif($this->existsFunction($name)){ UPDATE else return "already_exists"
– user60252
In fact, it would suffice only the part that is inside the function update Else (UPDATE) without the need of functions.
– user60252