Php 我什么时候可以使用isset和GET函数

Php 我什么时候可以使用isset和GET函数,php,Php,我正在学习如何用php创建一个电子商务网站的教程,目前我主要的疑问是什么时候必须使用isset函数 因此,首先,我使用以下代码在侧栏上显示所有产品品牌: function getBrands(){ global $con; $get_brands = "select * from brands"; $run_brands = mysqli_query($con, $get_brands); while ($row_brands=mysqli_fetch_arr

我正在学习如何用php创建一个电子商务网站的教程,目前我主要的疑问是什么时候必须使用isset函数

因此,首先,我使用以下代码在侧栏上显示所有产品品牌:

function getBrands(){
    global $con;

    $get_brands = "select * from brands";
    $run_brands = mysqli_query($con, $get_brands);

    while ($row_brands=mysqli_fetch_array($run_brands)){
         $brand_id = $row_brands['brand_id'];
         $brand_name = $row_brands['brand_name'];
         echo "<li><a href='index.php?brand=$brand_id'>$brand_name</a></li>";
    }
}
然后是显示基于品牌的所有产品的代码:

function get_brand_pro(){
    if(isset($_GET['brand'])){

        $brand_id = $_GET['brand'];

        global $con;

        $get_brand_pro = "select * from products where product_brand='$brand_id'"; 

        $run_brand_pro = mysqli_query($con, $get_brand_pro);

        $count_brands =  mysqli_num_rows($run_brand_pro);

        if($count_brands==0){
            echo "<h3>No products associated with this brand where foundd</h3>";
        }

        while( $row_brand_pro = mysqli_fetch_array($run_brand_pro)){

            $product_id = $row_brand_pro['product_id'];
            $product_cat = $row_brand_pro['product_cat'];
            $product_brand = $row_brand_pro['product_brand'];
            $product_title = $row_brand_pro['product_title'];
            $product_price = $row_brand_pro['product_price'];
            $product_image = $row_brand_pro['product_image'];

            echo "
                <div class='single_product'>    
                    <h3 class='product_title'>$product_title</h3>
                    <img src='admin-area/product_images/$product_image' alt='product image' width='250' height='250' />
                    <p class='product_price'>$ $product_price</p>
                    <div class='single_links'>
                        <a href='details.php?product_id=$product_id' class='details'>Details</a>
                        <a href='index.php?product_id=$product_id' class='add_cart'><button>Add to Cart</button></a>
                    </div>
                </div>

            ";
        }
    }
}
据我所知,isset函数的工作原理与单击按钮时相同

为什么选择isset[$\u GET['brands']]呢? 因为我看到数据库中没有名为brands的属性

希望您能纠正我的错误。

Well isset确定变量是否已设置或为空。在代码和数据库中,确实有一个名为product_brand的属性使用该值

您可以使用这一行获得值:$brand_id=$\u get['brand']; 然后在查询中使用它。

isset函数

isset函数用于检查是否设置了变量 不如果一个变量已经用unset函数取消设置,它将不会 更长的时间将被设定。isset函数在测试变量时返回false 包含空值

在代码中解释:


isset实际检查是否定义了变量或数组, 例如:

$name = $_GET["name"];
检查HTTP _GET的值是否已设置为变量$name的最佳方法是

if(empty($name)){
echo 'variable $name is empty';
}
但是,如果您想直接检查全局变量/array$\u GET,而不将其分配给变量,即当isset;功能是必需的。 范例


brands是在此处创建的查询字符串;因此,在另一页中,您首先需要检查品牌是否已设置,然后再尝试使用itbrand_id。。。itsand。。product_brand是使用product_brand=$brand_id获取的。我希望以上评论能澄清您的理解。你能确切说出你面临的问题吗?我们可以帮助找到解决办法。这是对空的可怕和荒谬的使用。
$name = $_GET["name"];
if(empty($name)){
echo 'variable $name is empty';
}
if(!isset($_GET["name"])){
echo '$_GET["name"] is not set';
}