PHP无法从我的数据库返回特定数据[已关闭]

tct7dpnv  于 2023-01-29  发布在  PHP
关注(0)|答案(1)|浏览(140)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

昨天关门了。
Improve this question
我设置了这个PHP,所以它从我的数据库中的多个表中获取信息。现在我的问题是,它没有显示任何东西,除了我的“错误”消息又名“类别未设置”。我厌倦了看这个,不明白我在哪里找到的。它必须检索产品名称+价格跨越4个表,食品,卫生,玩具,另外,它必须从表“产品”中检索这4个表的类别。

<?php
$servername = "localhost:3307";
$username = "root";
$password = "";
$dbname = "db_login";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
if(isset($category)){
  switch ($category) {
    case "Hygiene":
        $sql = "SELECT products.*, hygiene.product_id, hygiene.product_name, hygiene.price FROM products 
LEFT JOIN hygiene ON products.id = hygiene.product_id
WHERE products.category = 'Hygiene'";
        break;
    case "Food":
        $sql = "SELECT products.*, food.product_id, food.product_name, food.price FROM products 
LEFT JOIN food ON products.id = food.product_id
WHERE products.category = 'Food'";
        break;
    case "Toys":
        $sql = "SELECT products.*, toys.product_id, toys.product_name, toys.price FROM products 
LEFT JOIN toys ON products.id = toys.product_id
WHERE products.category = 'Toys'";
        break;
    case "Clothes":
        $sql = "SELECT products.*, clothes.product_id, clothes.product_name, clothes.price FROM products 
LEFT JOIN clothes ON products.id = clothes.product_id
WHERE products.category = 'Clothes'";
        break;
    default:
        echo "Invalid category";
        exit();
  }
}else{
    echo "Category is not set";
    exit();
}

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "<div class='".$row["category"]."-section'>";
        echo "<h5>".$row["category"]."</h5>";
        echo "<div class = 'col-sm card shadow--sm'>";
        echo "<img class = 'card--image' src='" . $row["image_path"] . "'>";
        echo "<p class ='card--text  card--buy--info'>". $row["product_name"];
        echo "<br>" . $row["price"] . " €" . "<i class='fa-solid fa-cart-shopping card--cart'></i>";
        echo "</div>";
        echo "</div>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>

它应该从我的表“产品”中获取类别,只有4个,并将其与其余信息一起显示在H5中。

hs1ihplo

hs1ihplo1#

你真的读过密码了吗?
在检查$category变量是否已设置之前,您在哪里定义它?
也许您的问题并不具体,但如果您希望从GET?或POST?获取$category值,则应考虑声明$category=$_GET['category']??"";,然后检查$category是否由if($category!=="") ...提交
阅读有关PHP中的“??”运算符(空合并运算符)的文档

相关问题