SQL没有插入

时间:2015-03-02 02:21:00

标签: php mysql sql pdo

好吧,所以我在过去的几天里为我的网站重写了一堆代码,出于某种原因,一个特定的代码没有正确插入:

如果删除where子句,代码(我在下面发布的内容)会完全触发。代码在之前已经解雇,并且继续在旧版本的代码中触发(如果需要我将发布,但看到SQL几乎完全相同,我不认为这是问题)。 / p>

function starMap()
{
    $x =$this->randomCoords();
    $y = $this->randomCoords();
    $px = mt_rand($this->limit_low, $this->limit_high);
    $py = mt_rand($this->limit_low, $this->limit_high);
    $sql = 
    "INSERT IGNORE INTO starinformation 
    (starOwner,  
     underSiege, 
     pageX, 
     pageY, 
     coordX, 
     coordY,
     resourceArray)
     values
     (0, 0, :pX, :pY, :cY, :cX, :r)
     WHERE 
        (coordX != {$x} AND coordY != {$y})
        OR
        (coordX != {$x}-5 AND coordY != {$y}-5)
        OR
        (coordX != {$x}+5 AND coordY != {$y}+5)
        AND
        pageX = {$px} AND pageY = {$py} 
     ;";
    $sql .= "INSERT INTO system_buildings(starID) VALUES (null);";
    $num = mt_rand($this->limit_low,$this->limit_high);
    $que = $this->db->prepare($sql);
    $que->bindParam(':cY', $y);
    $que->bindParam(':cX', $x);
    $que->bindParam(':pY', $py);
    $que->bindParam(':pX', $px);
    $que->bindParam(':r', $num);
    try { 
    $que->execute();
         }catch(PDOException $e)
            {
                echo $e->getCode().'|'.$e->getMessage();
            }
    $id =$this->db->lastInsertId();
    return $id;

}

1 个答案:

答案 0 :(得分:1)

INSERT语法不能包含WHERE子句。您使用INSERT语句时,唯一一次找到WHERE INSERT INTO...SELECT子句。

一个例子:

INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers; 

它会将SupplierName中的每条记录(CountrySupplier列)插入客户

如果你不相信我,你可以亲眼看到它Google