php - 调节从表中选择
问题描述
我有这个练习,我只需要列出当前不在购物车中的虚拟产品表中的产品。
我在创建购物车数组时发现了大问题(我已经通过_SESSION
不确定是否可行)并创建条件语句以仅选择未在提交后推送的购物车项目。我不知道我是否清楚,因为实际$cart
是一个数组数组,我想创建一个条件语句$sql= "SELECT * FROM products";
来检查是否是SELECT * FROM PRODUCTS
elseSELECT * FROM products WHERE 'id' NOT IN $cart
或类似的东西。
索引.php
<?php
require_once('common.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Index</title>
</head>
<body>
<?php while($product=$products->fetch(PDO::FETCH_ASSOC)):?>
<form method='post' action=''>
<input type='hidden' name='id' value="<?=$product['id']?>">
<table class='product'>
<thead>
<tr>
<th class='image' rowspan='3'><img src='img1.jpg' /></th>
<th class='title'><?=$product['title']?></th>
<th class='buy' rowspan='3'><button type='submit' name='add'>Add</button></th>
</tr>
<tr>
<td class='description'><?=$product['description']?></td>
</tr>
<tr>
<td class='price'><?=$product['price']?></td>
</tr>
</thead>
</table>
</form>
<?php endwhile;?>
<a href="./cart.php">
<button >Got to cart</button>
</a>
</body>
</html>
常见的.php
<?php
require_once('config.php');
//Connection display all products
try {
$con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
$sql= "SELECT * FROM `products`";
$products = $con->prepare($sql);
$products->execute();
}
catch(PDOException $exception){
echo "Connection error: " . $exception->getMessage();
}
if(isset($_POST['id']))
{
session_start();
$cart = array($_POST['id']);
$_SESSION['cart'] =count($_SESSION['cart'])>0?$_SESSION['cart']:array();
array_push($_SESSION['cart'],$cart);
$cart = $_SESSION['cart'];
print_r($cart);
};
解决方案
推荐阅读
- php - PHP ssh2_connect 通过代理
- python - 我的 Django 日志记录配置有什么问题?日志文件中看不到任何日志
- mongodb - MongoDB聚合很慢
- reactjs - 错误:最终加载程序没有返回缓冲区或字符串
- eclipse-plugin - 首次打开 MyDsl xtext 编辑器时出现 Guice 配置错误
- python - 如何将源代码添加到使用 sphinx 生成的 LaTeX 文档中
- javascript - 从对象构造函数之外的函数访问实例
- mysql - 选择过去 5 分钟内未选择的 1 行
- xml - 在根元素中添加命名空间以用于子元素的属性
- c# - 异步和等待总是返回“等待完成”