2016-07-26 3 views
0

클라이언트에 대한 정보를 저장하는 데이터베이스가 있습니다. 나는 "전화"라는 전화 번호를위한 독특한 필드를 가지고있다. 매개 변수화 된 쿼리를 사용하여 테이블에 INSERT하고 중복 키에 클라이언트 정보를 업데이트하려고합니다. 나는 철자법을 4 배 검사했고, 모든 것이 동등한 것처럼 보입니다. 계속 오류가 있습니다. "확인 ... 올바른 구문을 사용하는 곳 근처에서`Phone` =?"Parameterize Insert ... 중복 키 UP SET 의미를 사용하여 UPDATE가 실패합니다.

// These are the fields that we want to Upsert 
$fields = " 
    `Objectives` = :objectives, 
    `LiquidFunds` = :liquidFunds, 
    `UnitNumber` = :unitNumber, 
    `Accredited` = :accredited, 
    `FirstName` = :firstName, 
    `LastName` = :lastName, 
    `Street`  = :street, 
    `Phone`  = :phone, 
    `Email`  = :email, 
    `State`  = :state, 
    `Notes`  = :notes, 
    `City`  = :city, 
    `Zip`   = :zip 
"; 



$updateLead = $dbHandle -> prepare(" 
    INSERT INTO `Leads` SET $fields 
    ON DUPLICATE KEY UPDATE $fields WHERE `Phone` = :phoneKey" 
); 

그리고 나중에 나는 나는 같은에 최선을 다하는 노력했습니다

$updateLead = $dbHandle -> prepare("UPDATE `Leads` SET $fields WHERE `Phone` = :phoneKey); 

에 쿼리를 변경하면 너무

// Bind our parameters to the updateLead query 
$updateLead -> bindParam(":objectives" , $objectives); 
$updateLead -> bindParam(":liquidFunds", $_POST['liquidFunds']); 
$updateLead -> bindParam(":unitNumber" , $_POST['unitNumber']); 
$updateLead -> bindParam(":accredited" , $_POST['accredited']); 
$updateLead -> bindParam(":firstName" , $_POST['fname']); 
$updateLead -> bindParam(":lastName" , $_POST['lname']); 
$updateLead -> bindParam(":street"  , $_POST['street']); 
$updateLead -> bindParam(":phone"  , $_POST['phone']); 
$updateLead -> bindParam(":phoneKey" , $_POST['phone']); 
$updateLead -> bindParam(":email"  , $_POST['email']); 
$updateLead -> bindParam(":state"  , $_POST['state']); 
$updateLead -> bindParam(":notes"  , $_POST['notes']); 
$updateLead -> bindParam(":city"  , $_POST['city']); 
$updateLead -> bindParam(":zip"  , $_POST['zip']); 

이 정확한 구조가 완벽하게 작동과 같은 매개 변수를 바인딩하고 줄을 긋고 똑같은 오류를 던졌습니다 (다른 "줄 번호"에서). 내가 여기서 무엇을 잘못하고 있니?

PS : 나는 PHP/PDO

+1

'중복 키 업데이트'에 대한 설명서를 읽는 것이 맞습니까? –

+0

따라 할 수 있을지 모르겠다. –

답변

0

을 사용하고이 문제가 내가 쿼리에서 중복 자리를 가지고 있다는 사실이었다 밝혀졌습니다. 나는 이것을 다음과 같이 바꿨다

// These are the fields that we want to Upsert 
$fields = " 
    `Objectives` = :objectives, 
    `LiquidFunds` = :liquidFunds, 
    `UnitNumber` = :unitNumber, 
    `Accredited` = :accredited, 
    `FirstName` = :firstName, 
    `LastName` = :lastName, 
    `Fronter`  = :fronter, 
    `Street`  = :street, 
    `Phone`  = :phone, 
    `Email`  = :email, 
    `State`  = :state, 
    `Notes`  = :notes, 
    `City`  = :city, 
    `Zip`   = :zip 
"; 

$updateFields = " 
    `Objectives` = VALUES(Objectives), 
    `LiquidFunds` = VALUES(LiquidFunds), 
    `UnitNumber` = VALUES(UnitNumber), 
    `Accredited` = VALUES(Accredited), 
    `FirstName` = VALUES(FirstName), 
    `LastName` = VALUES(LastName), 
    `Fronter`  = VALUES(Fronter), 
    `Street`  = VALUES(Street), 
    `Phone`  = VALUES(Phone), 
    `Email`  = VALUES(Email), 
    `State`  = VALUES(State), 
    `Notes`  = VALUES(Notes), 
    `City`  = VALUES(City), 
    `Zip`   = VALUES(Zip) 
"; 


$updateLead = $dbHandle -> prepare(" 
    INSERT INTO `Leads` SET $fields 
    ON DUPLICATE KEY UPDATE $updateFields 
"); 

그리고 그것은 효과가 있었다!

+0

같은 placeholder는 괜찮습니다. 문제는이 쿼리가'WHERE'를 지원하지 않는다는 것입니다. –

+0

정말? .... 왜 내가 그럴 수 있니? MySQL은 업데이트 할 행을 자동으로 인식합니까? (그것은 이해할 만하다 ....). 어쨌든, 나는 phoneKey 매개 변수뿐만 아니라 WHERE 절도 제거했다. "잘못된 매개 변수 수"오류가 나타납니다. 나는 파기를 계속할 것이다. 팁 주셔서 감사합니다 어느 쪽이든 –

+0

'전화 번호'열은 고유합니다 - mysql은 확실히이 값을 가진 레코드를 알고 있습니다 –

관련 문제