我想避免在数据库中插入称为用户名的现有数据,我的代码运行没有错误,但是问题是即使我的数据库中已经存在输入的数据,也已插入。请告诉我我的代码缺少什么。
if($_POST["controller"] == "add") {
// validations here
$result = query("SELECT * FROM tbl_employee WHERE surname ='$surname'");
if(empty($_POST["surname"]) && (query($result)>0) ) {
echo 'surname already taken'; die();
}
else {
$age = getAge($_POST["dobMonth"], $_POST["dobDay"], $_POST["dobYear"]);
$result = query("INSERT INTO tbl_employee (carid, surname, firstname, position, department, birthplace, sex, address, contact, codename, birthdate, age, dateAdded) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", $_POST["modelo"], $_POST["surname"], $_POST["firstname"], $_POST["position"], $_POST["department"], $_POST["birthplace"], $_POST["sex"], $_POST["address"], $_POST["contact"], $_POST["codename"], $_POST["dobYear"] . '-' . $_POST["dobMonth"] . '-' . $_POST["dobDay"], $age, date("Y-m-d"));
if($result === false) {
echo 'error in insert'; die();
}
else {
$row = query("SELECT LAST_INSERT_ID() AS employee_id");
$id = $row[0]["employee_id"];
redirect("employee.php?profile=" . $id); //redirect("employee.php?action=view_all");
}
}
}
答案 0 :(得分:0)
尝试一下:希望对您有所帮助。
if($result['surname']==$_POST["surname"]) {
echo 'surname already taken'; die();
}
else {
$age = getAge($_POST["dobMonth"], $_POST["dobDay"], $_POST["dobYear"]);
$result = query("INSERT INTO tbl_employee (carid, surname, firstname, position, department, birthplace, sex, address, contact, codename, birthdate, age, dateAdded) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", $_POST["modelo"], $_POST["surname"], $_POST["firstname"], $_POST["position"], $_POST["department"], $_POST["birthplace"], $_POST["sex"], $_POST["address"], $_POST["contact"], $_POST["codename"], $_POST["dobYear"] . '-' . $_POST["dobMonth"] . '-' . $_POST["dobDay"], $age, date("Y-m-d"));
if($result === false) {
echo 'error in insert'; die();
}
答案 1 :(得分:0)
您只需要更改您的代码即可,希望对您有帮助。代码问题首先是它会检查姓氏值是否为空,如果您发送姓氏值将使条件变为假,因为“ AND”条件和您的else语句将执行
if($_POST["controller"] == "add") {
// validations here
$surname=(empty($_POST["surname"])?null:$_POST["surname"]);
$result = query("SELECT * FROM tbl_employee WHERE surname ='$surname'");
if(empty($_POST["surname"]) ){
echo 'surname is Empty'; die();
}
elseif((query($result)>0) )
{
echo 'surname already taken'; die();
}
else {
$age = getAge($_POST["dobMonth"], $_POST["dobDay"], $_POST["dobYear"]);
$result = query("INSERT INTO tbl_employee (carid, surname, firstname, position, department, birthplace, sex, address, contact, codename, birthdate, age, dateAdded) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", $_POST["modelo"], $_POST["surname"], $_POST["firstname"], $_POST["position"], $_POST["department"], $_POST["birthplace"], $_POST["sex"], $_POST["address"], $_POST["contact"], $_POST["codename"], $_POST["dobYear"] . '-' . $_POST["dobMonth"] . '-' . $_POST["dobDay"], $age, date("Y-m-d"));
if($result === false) {
echo 'error in insert'; die();
}
else {
$row = query("SELECT LAST_INSERT_ID() AS employee_id");
$id = $row[0]["employee_id"];
redirect("employee.php?profile=" . $id); //redirect("employee.php?action=view_all");
}
}
}
答案 2 :(得分:0)
您可能需要先检查它是否为 NULL ,然后再检查为空