2015-05-05 67 views
2

我有一個問題,我無法處理與準備語句重複的條目。PHP準備好的語句處理重複的條目

我想在出現重複條目​​時結束程序。這是我一直在努力做的事情:

function insert_vulnerability ($CVE, $Description, $Date, $Score, $Type){ 


    $conn = connection(); 

    $stmt = $conn->prepare("INSERT INTO Vulnerabilities (CVE, Description, Date, Score, Type) 
          VALUES (?, ?, ?, ?, ?)"); 




    $stmt->bind_param("sssis", $CVE, $Description, $Date, $Score, $Type); 

    if (false === $stmt) { 

     die('prepare() failed: ' . htmlspecialchars($mysqli->error)); 

    } 


    $stmt->execute(); 

    $conn->close(); 


} 

當不使用準備好的語句我這樣處理的錯誤,一切都非常完美:所以

function insert_vulnerability ($CVE, $Description, $Date, $Score, $Type){ 


    $conn = connection(); 

    $Description = htmlspecialchars($Description); 



    $sql = "INSERT INTO Vulnerabilities (CVE, Description, Date, Score, Type) 
    VALUES ('".$CVE."', '".$Description."', '".$Date."', '".$Score."', '".$Type."')"; 

    if ($conn->query($sql) === TRUE) { 
     //echo "New record created successfully"; 
    } else { 
     echo "Error: " . $sql . "<br>" . $conn->error; 
     $conn->close(); 
     die(); 

    } 

    $conn->close(); 

} 

如何獲取和預處理相同的結果陳述¿?

預先感謝您。

+0

那麼,什麼是你的問題?你可以提出有關重複的問題,現在你可以詢問錯誤處理。 –

+0

@u_mulder你是對的,我的壞。 –

回答

1

您可以連接$stmt->execute()內部的檢查來查看準備好的語句是否正常工作。

function insert_vulnerability ($CVE, $Description, $Date, $Score, $Type){ 
    $conn = connection(); 

    $stmt = $conn->prepare(' 
     INSERT INTO Vulnerabilities (CVE, Description, Date, Score, Type) 
     VALUES (?, ?, ?, ?, ?) 
    '); 

    $stmt->bind_param('sssis', $CVE, $Description, $Date, $Score, $Type); 
    if($stmt->execute()) { // true, success, else error 
     echo 'New record created successfully'; 
    } else { 
     echo $conn->error; 
    } 

    $conn->close(); 
} 

只是要注意,你有一個未定義的變量在你準備好的聲明中的一面:

$mysqli->error