2012-12-29 213 views
1

用戶發送sql查詢,有時(通常是服務器繁忙)sql行重複。我想阻止它。示例如果用戶同時發送相同的查詢,sql只接受一個查詢。我不想刪除它總是..我可以做到這一點在phpmyadmin?SQL防止重複輸入

如果我不能,問題在這個PHP: http://www.speedyshare.com/S4QRj/Automation.php

我想在這個代碼:

private function returnunitsComplete() { 
    global $database; 
    $time = time(); 
    $q = "SELECT * FROM ".TB_PREFIX."movement, ".TB_PREFIX."attacks where ".TB_PREFIX."movement.ref = ".TB_PREFIX."attacks.id and ".TB_PREFIX."movement.proc = '0' and ".TB_PREFIX."movement.sort_type = '4' and endtime < $time"; 
    $dataarray = $database->query_return($q); 


    foreach($dataarray as $data) { 

    $tribe = $database->getUserField($database->getVillageField($data['to'],"owner"),"tribe",0); 

    if($tribe == 1){ $u = ""; } elseif($tribe == 2){ $u = "1"; } elseif($tribe == 3){ $u = "2"; } elseif($tribe == 4){ $u = "3"; } else{ $u = "4"; } 
    $database->modifyUnit(
      $data['to'], 
      array($u."1",$u."2",$u."3",$u."4",$u."5",$u."6",$u."7",$u."8",$u."9",$tribe."0","hero"), 
      array($data['t1'],$data['t2'],$data['t3'],$data['t4'],$data['t5'],$data['t6'],$data['t7'],$data['t8'],$data['t9'],$data['t10'],$data['t11']), 
      array(1,1,1,1,1,1,1,1,1,1,1) 
    ); 
    $database->setMovementProc($data['moveid']); 
    } 

有時返回複製單位和報告。對不起,我的英語不好。謝謝...

+1

你看過'UNIQUE'列索引嗎? – deceze

+0

當你說查詢意味着要插入數據庫的數據庫或數據的問題? – HMarioD

回答

2

在您的表格中使用CONSTRAINT,如PRIMARY KEY和/或UNIQUE KEY。他們不會接受相同的價值。

CREATE TABLE Persons 
(
P_Id int NOT NULL, 
LastName varchar(255) NOT NULL, 
PRIMARY KEY (P_Id) //primary key 
) 

如需更多信息,請參閱here

+0

你能解釋一下嗎?我能怎麼做? –

+0

你需要在你的表結構中定義它。 – Ravi

+0

@ user1936293查看我更新的帖子 – Ravi

2

如果你想確保對列col1和表tbl1不包含重複的值對的col2的(但每列自己可以),您可以創建必要的唯一索引

ALTER TABLE tbl1 
    ADD UNIQUE KEY(col1, col2) 

文檔(MySQL):http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

+0

語法是'ALTER TABLE ... ** ADD ** UNIQUE KEY ...' –

+0

@ypercube fixed,thanks –