【发布时间】:2013-01-18 05:16:08
【问题描述】:
我正在尝试创建一个购物网站,我在该网站上销售价格每天都在波动的商品(贵金属)。我有一个表(产品),其中包含每个产品的乘数(类似于“1.1”)。我基本上不想每天走进我的桌子并更改数百件商品的价格。我的想法是创建另一个表,我将在其中简单地使用每天的每日价值更改价格字段。我怎样才能使最终产品价格的总和是一个表中的乘数乘以另一个表中输入的每日价格。或者,有没有比使用两个表更好的方法?到目前为止,这里的编码只使用了一个具有定义价格的表:
if (isset($_GET['id'])) {
//Connect To Mysql Database
include"storescripts/connect_to_mysql.php";
$id = preg_replace('#[^0-9]#i','',$_GET['id']);
//Use This VAR To Check To See If This ID Exists, If Yes Then Get Product
//Details, If No Then Exit Script and Give Message Why
$sql = mysql_query("SELECT * FROM products WHERE id='$id' LIMIT 1");
$productCount = mysql_num_rows($sql);
if ($productCount > 0) {
//Get All The Product Details
while ($row = mysql_fetch_array($sql)) {
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
$category = $row["category"];
$subcategory = $row["subcategory"];
$date_added = strftime("%b %d, %Y",strtotime($row["date_added"]));
}
} else {
echo "That Item Does Not Exist";
exit();
}
} else {
echo "Data To Render This Page Is Missing";
exit();
}
mysql_close();
【问题讨论】:
-
Please, don't use
mysql_*functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解prepared statements,并使用PDO 或MySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial. -
首先,停止使用mysql_query,看看mysli或者PDO。也就是说,为了简化事情,你可以让你的查询做数学运算,比如
SELECT *,product_price` * $product_multipler asweighted_priceFROM products WHERE id='$id' LIMIT 1`。 -
@j0k +1 您是否有该评论随时可用,然后您只需将其复制并粘贴到您看到这种不负责任行为的任何地方?对你有好处!
-
@JosephSilber 我只是从这里抓取它stackoverflow.com/a/12860140/569101 ;)
-
@j0k - 太好了!保持良好的工作。我们这里需要一些警力:)