0

此代码转到“Else”,我得到“字段列表中的列'服务器'不明确”

$otoon = mysql_real_escape_string($_POST['otoon']);
$oserver = mysql_real_escape_string($_POST['oserver']);
$server = mysql_real_escape_string($_POST['server']);

$sql="UPDATE regusers_test
INNER JOIN alt_toon_test ON (alt_toon_test.toonname = regusers_test.toonname)
SET server='$server'
WHERE username='$user' AND server='$oserver' AND toonname='$otoon'";

if (!empty($server)) {
  if (mysql_query($sql,$con)) {
    echo "Server updated to: " . $server;
  } else {
        echo "<h1>Error</h1>";
}
mysql_close($con);

我什至尝试将 $sql 更改为

$gql="UPDATE regusers_test, alt_toon_test SET server='$server' WHERE username='$user' AND server='$oserver' AND toonname='$otoon'";

但我仍然得到同样的错误。我在这里用头撞墙。我知道是什么导致了错误。我有 2 个同名字段。我只是不知道如何克服这一点。

4

1 回答 1

1

您必须在 SET 和 WHERE 子句中指定表名。也就是说,像这样:

$sql="更新 regusers_test
INNER JOIN alt_toon_test ON (alt_toon_test.toonname = regusers_test.toonname)
SET regusers_test.server='$server'
WHERE regusers_test.username='$user' AND regusers_test.server='$oserver' AND regusers_test.toonname='$otoon'";

如果您使用别名,查询会更短:

$sql="更新 regusers_test a
INNER JOIN alt_toon_test b ON (b.toonname = a.toonname)
SET a.server='$server'
WHERE a.username='$user'
  AND a.server='$oserver'
  AND a.toonname='$otoon'";

您还应该考虑切换到 mysqli 或 PDO 扩展,因为 mysql_query()已被弃用,并将在 PHP 版本的未来更新中删除。

于 2013-07-02T21:19:12.790 回答