2014-11-01 66 views
-3

喜在此代碼它顯示錯誤: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(); 
?> 
+0

不要添加具有相同主鍵的兩行。順便說一句,你的查詢是廣泛開放的SQL注入。 – 2014-11-01 10:17:12

+0

我想reg_no作爲唯一我爲什麼要刪除一個 – user 2014-11-01 11:43:31

+0

是它獨特的 – user 2014-11-01 12:03:58

回答

0

可能是你用來作爲reg_no在這種情況下,主鍵或唯一的,如果你嘗試添加已經存在的記錄(註冊號),它表明錯誤。

+0

然後我刪除也呈現出同樣的錯誤 – user 2014-11-01 10:19:35

+0

如何解決我的問題 – user 2014-11-01 10:25:21

+0

然後給你試圖插入reg_no和 檢查查詢SELECT reg_no,COUNT(reg_no )從patient_registration WHERE reg_no ='your_reg_no'和後輸出。 – SSRaavi 2014-11-01 10:29:22

0

在您的表reg_noprimary key。您正嘗試向註冊表添加重複註冊號碼的條目。使reg_no字段爲auto_increment,並將reg_no保留爲insert。 新的查詢看起來像

INSERT INTO patient_registration(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('$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')"; 
+0

reg_no不是主鍵 – user 2014-11-01 10:45:25

+0

是唯一的嗎? – Bibin 2014-11-01 10:50:03

相關問題