我遇到的问题是将SteamID和vac添加到数据库中,其他所有工作都无法获得这两项。这是我的代码。到数据库的连接信息在config.php中我连接正确插入没有任何问题。事实上它现在根本没有插入。
<?php
require 'config/config.php';
class SteamProfile{
const STEAMAPI_URL_MASK = 'http://steamcommunity.com/profiles/%s/?xml=1';
const UNKONWN_NAME_MASK = 'User #%s (Username Not Found)';
private $steamId;
private $xml;
public function __construct($steamId){
$this->steamId = $steamId;
}
public function getUsername(){
$xml = $this->getXml($this->steamId);
return $xml->steamID ? (string)$xml->steamID : sprintf(self::UNKONWN_NAME_MASK, $this->steamId);
}
private function getXml($steamId){
if ($this->xml){
return $this->xml;
}
$url = sprintf(self::STEAMAPI_URL_MASK, $steamId);
if (!$xml = simplexml_load_file($url)){
throw new UnexpectedValueException(sprintf('Unable to load XML from "%s"', $url));
}
return $this->xml = $xml;
}
public function getVAC(){
$xml = $this->getXml($this->steamId);
return $xml->vacBanned;
}
public function __toString(){
return sprintf("%s (SteamID: %s)", $this->getUsername(), $this->steamId);
}
}
if (empty($_POST)){
?>
<form name="banlist" action="index.php" method="POST">
<label for "SteamID">Steam64ID: </label><input type="text" name="SteamID"/><br />
<label for "bandate">Ban Until: </label><input type="text" name="bandate"/><br />
<button type="submit">Submit</button>
</form>
<?php
}
else{
$form = $_POST;
$SteamID = $form['SteamID'];
$profile = new SteamProfile($SteamID);
$namecheck = $profile->getUsername();
$vac = $profile->getVAC();
$bandate = $form['bandate'];
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname;",$username,$password);
$sql = "INSERT INTO PlayerBans (SteamID, playername, vac, bandate)VALUES(:SteamID, :namecheck, :vac, :bandate)";
$query = $dbh->prepare($sql);
$result = $query->execute(array(':SteamID'=>$SteamID, ':namecheck'=>$namecheck, ':vac'=>$vac, ':bandate'=>$bandate));
if ($result){
echo "Thanks for showing us this ban!";
} else {
echo "Sorry, an error occurred while editing the database. Contact the guy who built this garbage.";
}
}
?>
我不知道出了什么问题,因为我不习惯尝试使用PDO