PHP / MySQL - 更新查询
PHP / MySQL - Updating queries
我正在尝试修复 CS-Cart 中的一些 SQL 查询,以便更好地使用它们的内置查询,以下工作以及进一步的代码循环和显示产品
$getOrder=db_get_fields("SELECT * FROM ?:orders WHERE total='".$orderSum."' AND email='".$email."' AND status IN('P','C','O') ");
$orderProducts = db_get_fields("SELECT product_id FROM cscart_order_details WHERE order_id='".$getOrder[0]."'");
但我尝试使用以下方式编写:
http://docs.cs-cart.com/4.2.x/core/db/placeholders.html
然而,它只有 returns 1 个产品,所以我在某个地方犯了错误,但不确定是什么?谁有想法?我对 PHP 和 MySQL
比较陌生
$getOrder = db_get_fields("SELECT * FROM ?:orders WHERE total = ?i AND email = ?s AND status IN ('P','C','O')", $orderSum, $email);
$orderProducts = db_get_fields("SELECT product_id FROM ?:order_details WHERE order_id = ?a", $getOrder[0]);
循环更新并检查:
$acId = $_REQUEST['id'];
$productsGet = db_get_field("SELECT cart FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
$products = unserialize($productsGet);
$cartProducts=array();
if (is_array($products) || is_object($products)){
foreach($products as $pData){
$cartProducts[]=$pData['product_id'];
}
}
if(!array_diff($cartProducts,$orderProducts)){
$products=array();
}
哦,这是输出部分,但它是新查询的问题
if (!empty($products)) {
foreach ($products as $product) {
$text .='
<tr>
<td><a href="http://'.$_SERVER['SERVER_NAME'].'?dispatch=products.view&product_id='.$product['product_id'].'"> <img title="" height="120" width="120" alt="" src="'.$product['main_pair']['detailed']['image_path'].'"></a></td>
<td><a href="#" style=" font-weight:bold; color:#333; font-size:13px; text-decoration:none;">'.$product['product'].'</a><a href="#"> <i></i></a><div style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;"> CODE: <span>'.$product['product_code'].'<!--product_code_update_2512012004--></span> </div></td>
<td style=" text-align:center;"><span style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;">$</span><span style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;">'.$product['price'].'</span> </td>
<td><div style="display: inline-block;vertical-align: top;width: 56px;"><input type="text" disabled value="'.$product['amount'].'" size="3" style="border:1px solid #c2c9d0; box-shadow:0 1px 3px rgba(0, 0, 0, 0.1) inset; border-radius:3px; float: left;height: 33px;text-align: center;width: 36px;"></div></td>
<td style="font-size:14px; font-weight:bold; color:#333; text-align:center; font-size:13px; text-decoration:none;"><span>$</span><span stye=" color:#000;">'.$product['price']*$product['amount'].'</span> </td>
</tr>';
$sum =$sum+$product['price']*$product['amount'];
}
}
好的,找到问题了,而且很简单
$orderProducts = db_get_fields("SELECT product_id FROM ?:order_details WHERE order_id = ?a", $getOrder[0]);
至
$orderProducts = db_get_fields("SELECT product_id FROM ?:order_details WHERE order_id = ?s", $getOrder[0]);
?a > ?s
$getOrder = db_get_fields("SELECT * FROM ?:orders WHERE total = ?i AND email = ?s AND status IN ('P','C','O')", $orderSum, $email);
$orderProducts = db_get_fields("SELECT product_id FROM ?:order_details WHERE order_id = ?a", $getOrder[0]);
在第一个查询中,您使用的是 ?i,它表示一个整数,但是 CS-Cart 将总计存储为一个浮点数。这不是最好的解决方案。
您的第一个查询应该是:
$getOrder = db_get_row("SELECT * FROM ?:orders WHERE total = ?i AND email = ?s AND status IN ('P','C','O')", $orderSum, $email);
第二个:
$orderProducts = db_get_row("SELECT product_id FROM ?:order_details WHERE order_id = ?i", $getOrder['order_id']);
此外,我建议你使用db_get_row()
函数,如果你只想从数据库中获取1行,因为CS-Cart会将其转换为关联数组,其中键是字段名字。因此您的代码将更具可读性。
如果你查看官方文档,你可以使用 ?a 如果你想在一个集合中找到一些东西。例如。 db_query('SELECT * FROM ?:orders WHERE order_id IN (?a)', $order_id);
我正在尝试修复 CS-Cart 中的一些 SQL 查询,以便更好地使用它们的内置查询,以下工作以及进一步的代码循环和显示产品
$getOrder=db_get_fields("SELECT * FROM ?:orders WHERE total='".$orderSum."' AND email='".$email."' AND status IN('P','C','O') ");
$orderProducts = db_get_fields("SELECT product_id FROM cscart_order_details WHERE order_id='".$getOrder[0]."'");
但我尝试使用以下方式编写:
http://docs.cs-cart.com/4.2.x/core/db/placeholders.html
然而,它只有 returns 1 个产品,所以我在某个地方犯了错误,但不确定是什么?谁有想法?我对 PHP 和 MySQL
比较陌生$getOrder = db_get_fields("SELECT * FROM ?:orders WHERE total = ?i AND email = ?s AND status IN ('P','C','O')", $orderSum, $email);
$orderProducts = db_get_fields("SELECT product_id FROM ?:order_details WHERE order_id = ?a", $getOrder[0]);
循环更新并检查:
$acId = $_REQUEST['id'];
$productsGet = db_get_field("SELECT cart FROM ?:abandoned_cart WHERE user_id = ?s", $acId);
$products = unserialize($productsGet);
$cartProducts=array();
if (is_array($products) || is_object($products)){
foreach($products as $pData){
$cartProducts[]=$pData['product_id'];
}
}
if(!array_diff($cartProducts,$orderProducts)){
$products=array();
}
哦,这是输出部分,但它是新查询的问题
if (!empty($products)) {
foreach ($products as $product) {
$text .='
<tr>
<td><a href="http://'.$_SERVER['SERVER_NAME'].'?dispatch=products.view&product_id='.$product['product_id'].'"> <img title="" height="120" width="120" alt="" src="'.$product['main_pair']['detailed']['image_path'].'"></a></td>
<td><a href="#" style=" font-weight:bold; color:#333; font-size:13px; text-decoration:none;">'.$product['product'].'</a><a href="#"> <i></i></a><div style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;"> CODE: <span>'.$product['product_code'].'<!--product_code_update_2512012004--></span> </div></td>
<td style=" text-align:center;"><span style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;">$</span><span style=" font-weight:bold; color:#333; font-size:12px; margin-top:4px; text-decoration:none;">'.$product['price'].'</span> </td>
<td><div style="display: inline-block;vertical-align: top;width: 56px;"><input type="text" disabled value="'.$product['amount'].'" size="3" style="border:1px solid #c2c9d0; box-shadow:0 1px 3px rgba(0, 0, 0, 0.1) inset; border-radius:3px; float: left;height: 33px;text-align: center;width: 36px;"></div></td>
<td style="font-size:14px; font-weight:bold; color:#333; text-align:center; font-size:13px; text-decoration:none;"><span>$</span><span stye=" color:#000;">'.$product['price']*$product['amount'].'</span> </td>
</tr>';
$sum =$sum+$product['price']*$product['amount'];
}
}
好的,找到问题了,而且很简单
$orderProducts = db_get_fields("SELECT product_id FROM ?:order_details WHERE order_id = ?a", $getOrder[0]);
至
$orderProducts = db_get_fields("SELECT product_id FROM ?:order_details WHERE order_id = ?s", $getOrder[0]);
?a > ?s
$getOrder = db_get_fields("SELECT * FROM ?:orders WHERE total = ?i AND email = ?s AND status IN ('P','C','O')", $orderSum, $email);
$orderProducts = db_get_fields("SELECT product_id FROM ?:order_details WHERE order_id = ?a", $getOrder[0]);
在第一个查询中,您使用的是 ?i,它表示一个整数,但是 CS-Cart 将总计存储为一个浮点数。这不是最好的解决方案。
您的第一个查询应该是:
$getOrder = db_get_row("SELECT * FROM ?:orders WHERE total = ?i AND email = ?s AND status IN ('P','C','O')", $orderSum, $email);
第二个:
$orderProducts = db_get_row("SELECT product_id FROM ?:order_details WHERE order_id = ?i", $getOrder['order_id']);
此外,我建议你使用db_get_row()
函数,如果你只想从数据库中获取1行,因为CS-Cart会将其转换为关联数组,其中键是字段名字。因此您的代码将更具可读性。
如果你查看官方文档,你可以使用 ?a 如果你想在一个集合中找到一些东西。例如。 db_query('SELECT * FROM ?:orders WHERE order_id IN (?a)', $order_id);