我正在为 Magic the Gathering Cards 创建库存系统,需要使用主卡信息更新价格。
我有两张桌子,卡片和价格
卡片具有以下列:ID、名称、Ed、价格
价格具有以下列:名称、Ed、价格
我需要将 Cards.Price 替换为 Price.Price 中的值。
下面是完成这项工作的两种不同尝试的代码(我知道我可能使这比它需要的更难......)
尝试#1
$query = "SELECT * FROM Cards";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$name=$row['Name'];
$ed=$row['Ed'];
$queryb = "SELECT Price FROM Prices WHERE Name='".$name."' AND Ed='".$ed."'";
$resultb = mysql_query($queryb) or die(mysql_error());
$rowb = mysql_fetch_array($resultb) or die(mysql_error());
$newPrice = $rowb['Price'];
mysql_query("UPDATE Cards SET Price='".$newPrice."'");
}
尝试#2
$queryCards = "SELECT * FROM Cards";
$queryPrices = "SELECT * FROM Prices";
$dblink = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $dblink);
$resultCards = mysql_query($queryCards) or die(mysql_error());
$resultPrices = mysql_query($queryPrices) or die(mysql_error());
$rowCards = mysql_fetch_array($resultCards) or die(mysql_error());
$rowPrices = mysql_fetch_array($resultPrices) or die(mysql_error());
if ($rowCards['Name']==$rowPrices['Name'] && $rowCards['Ed']==$rowPrices['Ed'])
{
$newPrice = $rowPrices['Price'];
mysql_query("UPDATE Cards SET Price='".$newPrice."' WHERE
Name='".$rowCards['Name']."' AND Ed='".$rowCards['Ed']."'");
}
while($rowPrices = mysql_fetch_array($resultPrices))
{
if ($rowCards['Name']==$rowPrices['Name'] &&
$rowCards['Ed']==$rowPrices['Ed'])
{
$newPrice = $rowPrices['Price'];
mysql_query("UPDATE Cards SET Price='".$newPrice."' WHERE
Name='".$rowCards['Name']."' AND Ed='".$rowCards['Ed']."'");
}
}
$rowPrices = mysql_fetch_array($resultPrices) or die(mysql_error());
while($rowCards = mysql_fetch_array($resultCards))
{
while($rowPrices = mysql_fetch_array($resultPrices))
{
if ($rowCards['Name']==$rowPrices['Name'] &&
$rowCards['Ed']==$rowPrices['Ed'])
{
$newPrice = $rowPrices['Price'];
mysql_query("UPDATE Cards SET Price='".$newPrice."' WHERE
Name='".$rowCards['Name']."' AND Ed='".$rowCards['Ed']."'");
}
}
}