如何从两个 MySQL 数据库填充一个简单的购物车?
我正在开发一个简单的 PHP 购物车,总体进展顺利。我遇到的困难是能够从两个单独的数据库中提取数据,以便填充购物车的字段(单价、名称、数量等)。
这是 showCart 函数:
function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM table_name WHERE id ='. $id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$product_name.' by '.$country.'</td>';
$output[] = '<td>$'.$price.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>$'.($price * $qty).'</td>';
$total += $price * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Grand total: <strong>$'.$total.'</strong></p>';
$output[] = '<div><button type="submit">Update cart</button></div>';
$output[] = '</form>';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
购物车工作正常,如上所示。您可以看到我已经从 MySQL 数据库表中提取了 id、价格、产品名称和其他一些信息。现在,我想让价格来自不同的数据库表。为此,我尝试了以下操作...
首先,我创建了一个新连接,它将获取我想要的价格值(Get_Price.php):
include 'connect.php';
$actual_price='_POST['new_price']';
$offer_price = mysql_connect("$host", "$username", "$password") or die ("Unable to connect to server");
mysql_select_db("$database", $offer_price) or die ("Unable to select database");
$get_value=mysql_query("SELECT * FROM offers WHERE actual_price = '$actual_price'");
$value_rslt = $db->query($get_value);
$vlu_row = $value_rslt->fetch();
extract($vlu_row);
$vlu_otp[] = '<td>$'.$special_offer.'</td>';
$vlu_otp[] = '<td><input type="text" name="qty'.$unique_id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$vlu_otp[] = '<td>$'.($special_offer * $qty).'</td>';
$total += $special_offer * $qty;
$vlu_otp[] = '</tr>';
然后我使用此调用:
include 'Get_Price.php';
作为原始代码中的替代:
function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM table_name WHERE id ='. $id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$product_name.' by '.$country.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
include 'Get_Price.php';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
这创建了一个大量错误并且它删除了所有值。解决办法是什么?
I am developing a simple PHP shopping cart and overall it is going well. The difficulty that I am having is in being able to pull data from two separate databases in order to populate the fields of the shopping cart (unit price, name, quantities, etc...).
Here is the showCart function:
function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM table_name WHERE id ='. $id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$product_name.' by '.$country.'</td>';
$output[] = '<td>$'.$price.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>$'.($price * $qty).'</td>';
$total += $price * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Grand total: <strong>$'.$total.'</strong></p>';
$output[] = '<div><button type="submit">Update cart</button></div>';
$output[] = '</form>';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
The cart works fine as it is shown above. You can see that I have pulled the id, price, product name and a few other bits from the MySQL database table. Now, I would like to make the price come from a different database table. To do this, I tried the following...
First, I created a new connection which will get the price value I am after (Get_Price.php):
include 'connect.php';
$actual_price='_POST['new_price']';
$offer_price = mysql_connect("$host", "$username", "$password") or die ("Unable to connect to server");
mysql_select_db("$database", $offer_price) or die ("Unable to select database");
$get_value=mysql_query("SELECT * FROM offers WHERE actual_price = '$actual_price'");
$value_rslt = $db->query($get_value);
$vlu_row = $value_rslt->fetch();
extract($vlu_row);
$vlu_otp[] = '<td>$'.$special_offer.'</td>';
$vlu_otp[] = '<td><input type="text" name="qty'.$unique_id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$vlu_otp[] = '<td>$'.($special_offer * $qty).'</td>';
$total += $special_offer * $qty;
$vlu_otp[] = '</tr>';
Then I used this call:
include 'Get_Price.php';
as a substitute in the original code:
function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM table_name WHERE id ='. $id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$product_name.' by '.$country.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
include 'Get_Price.php';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
This created a ton of errors and it dropped all the values. What is the solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,尝试看看是否可以使用单个数据库来实现此目的。
如果没有,请查看
Get_Price.php
。您正在尝试连接到此处的第二个数据库并使用变量$offer_price
。但是,当您针对第二个数据库运行查询时,您使用的是$db->query($get_value);
,它实际上将针对第一个数据库运行。解决方案:在
connect.php
中创建两个数据库连接 - 类似于$db_cart
和$db_price
。然后使用相关连接照常进行。你的代码的其余部分看起来不错。First, try and see if you could use a single database for this.
If not, take a look at
Get_Price.php
. You are trying to connect to the second database here and use the variable$offer_price
for it. But, when you are running the query against the second database, you are using$db->query($get_value);
which will actually run against the first database.Solution: Create two database connections in
connect.php
- something like$db_cart
and$db_price
. Then proceed as usual using the relevant connection. The rest of your code seems fine.