为什么我的PHP / MySQL插入而不是更新?

时间:2013-03-14 23:34:17

标签: php mysql insert sql-update

if ($Funct == "BUILD" AND $Mode == "GROUND")
{
    $sql = "SELECT * FROM $landCon WHERE North = '" . $North . "' AND West = '" . $West ."'" ;
    $result = mysql_query($sql) or die("Error occurred in [$sql]: " . mysql_error());
    $count = mysql_num_rows($result);

    If ($Count == 0 )
    {
        $sql = "INSERT INTO $landCon (West,North,Type1) VALUES ($West,$North,'$Char')";
        mysql_query($sql) or die("Error occurred in RootA as:[$sql]: " . mysql_error());

    }
    else
    {
        $result=mysql_query("UPDATE $landCon SET Type1= '$Char' WHERE North = $North AND West=$West") or die( "Database Error: ".mysql_error());
    }
    echo "SUCCESS";
}

而不是更新条目存在的位置,它正在插入。我的意图是,如果已经指定了$North$South条目,则更新它,以便每个地图网格只有一个北/南值。

1 个答案:

答案 0 :(得分:6)

$count = mysql_num_rows($result);

If ($Count == 0 )

$count $Count

验证码:在您输入拼写错误时静默初始化新变量的语言