SQL防止重复输入

时间:2012-12-29 11:39:27

标签: php sql duplicates

用户发送sql查询,有时(通常是服务器忙)sql行重复。我想防止它。例如,如果用户同时发送相同的查询,则sql只接受一个查询。我不想一直删除它..我能在phpmyadmin上做吗?

如果我不能解决这个问题: 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']);
    }

有时返回单位和报告重复。对不起,我的英语不好。谢谢......

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

如果您想确保表col1的{​​{1}}和col2列不包含重复的值对(但每列都可以),您可以创建必要的唯一索引

tbl1

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