2016-12-21 131 views
0

現在,我點擊一個添加按鈕時會彈出一個對話框。內部有2個輸入,將在提交時插入數據庫。有一個下拉列表和一個輸入框。如果已經存在於數據庫中的值輸入到輸入框中,我不希望提交對話框能夠工作。所以基本上,我不希望Supp_ID列中有任何重複的記錄。我怎樣才能做到這一點?這是我到目前爲止。輸入重複記錄時不要插入到數據庫

對話形式:

<div id="dialog-form" title="Add Supplier ID"> 
    <p class="validateTips">All form fields are required.</p> 

<!-- Dialog box displayed after add row button is clicked --> 
    <form > 
    <fieldset> 
     <label for="mr_id">MR_ID</label> 
     <select name="mr_id" id="mr_id_dialog" class="text ui-widget-content ui-corner-all" value="300"> 
      <?php foreach($user1->fetchAll() as $user2) { ?> 
      <option> 
       <?php echo $user2['MR_ID'];?> 
      </option> 
     <?php } ?> 
     </select><br><br> 
     <label for="supplier_id">Supplier ID</label> 
     <input type="text" name="supp_id" id="supplier_id" class="text ui-widget-content ui-corner-all" value="99"> 

     <!-- Allow form submission with keyboard without duplicating the dialog button --> 
     <input type="submit" id="submit" tabindex="-1" style="position:absolute; top:-1000px"> 
    </fieldset> 
    </form> 
</div> 

的JavaScript:

$("document").ready(function() { 
     $('#submit').submit(function() { 
       processDetails(); 
       return false; 
     }); 
}); 

function processDetails() { 
     var errors = ''; 

     // Validate Supp ID 
     var supplier = $("#supplier_id [name='supp_id']").val(); 
     if (supplier == "null" || supplier == "") { // check for empty value 
       errors += ' - Please enter a different Supplier ID\n'; 
     } 
     // MORE FORM VALIDATIONS 
     if (errors) { 
       errors = 'The following errors occurred:\n' + errors; 
       alert(errors); 
       return false; 
     } else { 
       // Submit form via Ajax and then reset the form 
       $("#submit").ajaxSubmit({success:showResult}).resetForm(); 
       return false; 
     } 
} 

function showResult(data) { 
     if (data == 'save_failed') { 
       alert('ERROR. Your input was not saved.'); 
       return false; 
     } else if (data == 'save_failed_duplicate') { 
       alert('ERROR. Input data already exists.'); 
       return false; 
     } else { 
       alert('SUCCESS. Your input data has been saved.'); 
       return false; 
     } 
} 

Insert.php

<?php 
$MR_ID = $_POST['MR_ID']; 
$Supp_ID = $_POST['Supp_ID']; 

    $host="xxxxxxxx"; 
    $dbName="xxxx"; 
    $dbUser="xxxxxxxxxxx"; 
    $dbPass="xxxxxxxxx"; 

    $pdo = new PDO("sqlsrv:server=".$host.";Database=".$dbName, $dbUser, $dbPass); 
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 


$check_sql = "SELECT Supp_ID FROM Stage_Rebate_Index WHERE Supp_ID = '$Supp_ID'"; 
$check_sql_query = sqlsrv_query($check_sql, $dbh); 
if (sqlsrv_num_rows($check_sql_query) > 0) { 
     echo "save_failed_duplicate"; 
     @sqlsrv_close($dbh); 
     return; 
} else { 
    if (sqlsrv_num_rows($check_sql_query) == 0) { 
     $sql = "INSERT INTO Stage_Rebate_Index (MR_ID, Supp_ID) VALUES (?, '$Supp_ID')"; 
     if (@sqlsrv_query($sql, $dbh)) { 
       echo "success"; 
       @sqlsrv_close($dbh); 
       return; 
     } else { 
       echo "save_failed"; 
       @sqlsrv_close($dbh); 
       return; 
     } 
    } 
} 

    $stmt = $pdo->prepare($sql); 
    $result = $stmt->execute(array($MR_ID, $Supp_ID)); 
    echo json_encode($result); 


?> 
+1

化妝** Supp_ID列**在數據庫中是唯一和使用** UPSERT查詢到**避免重複的值。 –

回答

0

使用IGNORE關鍵字在你的SQL語句。

$sql = "INSERT IGNORE INTO Stage_Rebate_Index (MR_ID, Supp_ID) VALUES (?, '$Supp_ID')";

您將需要修改你的迴應檢查的影響,而不是查詢是否失敗的行數。

參見:http://php.net/manual/en/pdostatement.rowcount.php

1

我總是用ON DUPLICATE KEY UPDATE的情況下,我想更新時間戳或東西。

$sql = "INSERT INTO Stage_Rebate_Index (MR_ID, Supp_ID) VALUES (?, '$Supp_ID') ON DUPLICATE KEY UPDATE `MR_ID` = `MR_ID`"; 

基本上,它試圖插入記錄,但如果密鑰存在,它會更新你需要的任何數據。在上面的例子中,它只是更新MR_ID與原始MR_ID

這裏是前一個問題與此類似:Link

這裏是鏈接的MySQL手冊:Link

+0

我不想更新,但如果它是重複的,我只想讓它返回一個錯誤 – Rataiczak24

+1

然後,您需要添加一個唯一的索引到您嘗試獨特的列。如果唯一值已經存在,將會導致錯誤。 – john