喜在此代碼它顯示錯誤:MySQL的:1062:重複的項目「」關鍵「reg_no」
["1062: Duplicate entry '' for key 'reg_no'"]
任何一個可以請幫助我。爲什麼在添加名稱爲reg_no
的字段時顯示此錯誤重複條目。
我想返回true,如果查詢執行。
PHP
<?php
mysql_connect("localhost","root","");
mysql_select_db("hms");
//$student_name = !empty($_POST['student_name']) ? $_POST['student_name'] : $_GET['student_name'];
//$roll_no = !empty($_POST['roll_no']) ? $_POST['roll_no'] : $_GET['roll_no'];
$relation=!empty($_POST['relation']) ? $_POST['relation'] : $_GET['relation'];
$initial_id=!empty($_POST['initial_id']) ? $_POST['initial_id'] : $_GET['initial_id'];
$reg_no=!empty($_POST['reg_no']) ? $_POST['reg_no'] : $_GET['reg_no'];
$pmc=!empty($_POST['pmc']) ? $_POST['pmc'] : $_GET['pmc'];
$pc=!empty($_POST['pc']) ? $_POST['pc'] : $_GET['pc'];
$dob=!empty($_POST['dob']) ? $_POST['dob'] : $_GET['dob'];
$first_name=!empty($_POST['first_name']) ? $_POST['first_name'] : $_GET['first_name'];
$middle_name=!empty($_POST['middle_name']) ? $_POST['middle_name'] : $_GET['middle_name'];
$last_name=!empty($_POST['last_name']) ? $_POST['last_name'] : $_GET['last_name'];
$gender=!empty($_POST['gender']) ? $_POST['gender'] : $_GET['gender'];
$age=!empty($_POST['student_name']) ? $_POST['student_name'] : $_GET['student_name'];
$fh=!empty($_POST['fh']) ? $_POST['fh'] : $_GET['fh'];
$photo=!empty($_POST['photo']) ? $_POST['photo'] : $_GET['photo'];
$religion_id=!empty($_POST['religion_id']) ? $_POST['religion_id'] : $_GET['religion_id'];
$caste_id=!empty($_POST['caste_id']) ? $_POST['caste_id'] : $_GET['caste_id'];
$address=!empty($_POST['address']) ? $_POST['address'] : $_GET['address'];
$phone_office=!empty($_POST['phone_office']) ? $_POST['phone_office'] : $_GET['phone_office'];
$pin=!empty($_POST['pin']) ? $_POST['pin'] : $_GET['pin'];
$phone_resi=!empty($_POST['phone_resi']) ? $_POST['phone_resi'] : $_GET['phone_resi'];
$email=!empty($_POST['email']) ? $_POST['email'] : $_GET['email'];
$mobile_no=!empty($_POST['mobile_no']) ? $_POST['mobile_no'] : $_GET['mobile_no'];
$pmode=!empty($_POST['pmode']) ? $_POST['pmode'] : $_GET['pmode'];
$id_proof=!empty($_POST['id_proof']) ? $_POST['id_proof'] : $_GET['id_proof'];
$name_of_kin=!empty($_POST['name_of_kin']) ? $_POST['name_of_kin'] : $_GET['name_of_kin'];
$blood_group=!empty($_POST['blood_group']) ? $_POST['blood_group'] : $_GET['blood_group'];
$query="INSERT INTO patient_registration(reg_no,patient_main_type_id, patient_type_id,initial_id,first_name,middle_name,last_name,fth_mth_name,religion_id,date_of_birth,caste_id,address,pin,email,id_proof,phone_office, phone_residence,mobile_no,name_of_kin,relationship,entry_date,entry_time,patient_photo,payment_mode)
VALUES('$reg_no','$pmc','$pc','$initial_id','$first_name','$middle_name','$last_name','$fh','$religion_id','$dob','$caste_id','$address','$pin','$email','$id_proof','$phone_office','$phone_resi','$mobile_no','$name_of_kin','','','$relation','$photo','$pmode')";
$json_output = array();
if (!mysql_query($query)) {
$json_output[] = mysql_errno() . ": ".mysql_error();
}else{
$json_output[] = true;
}
print(json_encode($json_output));
mysql_close();
?>
不要添加具有相同主鍵的兩行。順便說一句,你的查詢是廣泛開放的SQL注入。 – 2014-11-01 10:17:12
我想reg_no作爲唯一我爲什麼要刪除一個 – user 2014-11-01 11:43:31
是它獨特的 – user 2014-11-01 12:03:58