php - Validate unique entry on edit/update -
i'm trying validate unique entry when editing/updating record. validating searching database, if entry not found proceed else print error message. if entry (i using same process when adding new records)
my validation code is:
if (!empty($_post['ip_add'])) { if (filter_var($_post['ip_add'], filter_validate_ip, filter_flag_ipv4) === false) { $errors[] = $_post['ip_add'] . ' not valid ipv4'; } else { $ip = $_post['ip_add']; //check if ip unique $qip = 'select inet_ntoa(ip_add) ip_add equipment ip_add = inet_aton(:ip)'; $database->query($qip); $database->bind(':ip', $ip); $rs = $database->resultset(); //execute our query $database->execute(); if ($rs != null) { $errors[] = $_post['ip_add'] . ' not unique'; } else { $ip_add = $_post['ip_add']; } } } else { $errors[] = "please enter valid ip address"; }
if there no errors update database:
$query = " update equipment set site_code = :site_code, site_id = :site_id, system_name = :system_name, ip_add = inet_aton(:ip_add), mcast = inet_aton(:mcast), sys_loc = :sys_loc, systype = :systype, itamname = :itamname, dimetis = :dimetis, dns = :dns id = :id "; //prepare query excecution $database->query($query); //bind parameters $database->bind(':site_code', $site_code); $database->bind(':site_id', $site_id); $database->bind(':system_name', $system_name); $database->bind(':ip_add', $ip_add); $database->bind(':mcast', $multicast); $database->bind(':sys_loc', $sys_loc); $database->bind(':systype', $systype); $database->bind(':itamname', $itamname); $database->bind(':dimetis', $_post['dimetis']); $database->bind(':dns', $_post['dns']); $database->bind(':id', $_post['id']); // execute query $database->execute(); echo "record updated."; } }
i thinking search on records execpt current record editing. how this? there better way checking if ip addrss unique when editing record?
i got work excluding current record search:
$qip = ' select inet_ntoa(ip_add) ip_add equipment ip_add = inet_aton(:ip) , id != :id '; $database->query($qip); $database->bind(':ip', $ip); $database->bind(':id', $id);
Comments
Post a Comment