如何检查 MySQL 中的字段以显示它是否为空?

How to check a field in MySQL to show if it's empty or no?

我在一家在线商店工作,我在尝试检索产品颜色并在 Html 复选框中向用户显示时遇到了一个大问题。我在我的数据库中插入了四个字段并将它们设置为变量 $pro_one, $pro_two, $pro_three,$pro_four。但问题是有些产品只有两种颜色,我不希望页面显示其他 2 个复选框。为此,我需要编写一个 If else 语句 来检查 phpmyadmin 中的字段,看看每个字段是否不为空,显示它,否则就不显示它。所以,如果你知道如何做到这一点,请帮助我,我真的很感激!谢谢...

<?php 
                    if (isset($_GET['pro_id'])){
                        $product_id = $_GET['pro_id'];
                        $get_pro = "select * from products where product_id='$product_id'";
                        $run_pro = mysqli_query($con,$get_pro);
                        while($row_pro = mysqli_fetch_array($run_pro)){

                            $pro_id = $row_pro['product_id'];
                            $pro_cat = $row_pro['product_cat'];
                            $pro_brand = $row_pro['product_brand'];
                            $pro_title = $row_pro['product_title'];
                            $pro_price = $row_pro['product_price'];
                            $pro_image = $row_pro['product_image'];
                            $pro_desc = $row_pro['product_desc'];
                            $pro_key = $row_pro['product_keywords'];
                            $pro_date = $row_pro['date_added'];
                            $pro_ava = $row_pro['input_ava'];
                            $pro_rate = $row_pro['site_rate'];
                            $pro_rev = $row_pro['product_review'];
                            $pro_plast = $row_pro['last_price'];
                            $pro_warran = $row_pro['product_warranty'];
                            $pro_pre = $row_pro['gooya_present'];
                            $pro_colors = $row_pro['ava_colors'];
                            $pro_one = $row_pro['color_one'];
                            $pro_two = $row_pro['color_two'];
                            $pro_three = $row_pro['color_three'];
                            $pro_four = $row_pro['color_four'];


echo "<form id='submittion'> 
                                    <label style='font-family: BMehrBold, Arial, Helvetica, sans-serif;'>Quantity:</label>
                                    <input type='text' value='1' /></br></br>
                                    <table style='margin-bottom:10px;width: 100%;'>
                                        <tr>
                                            <td><input type='checkbox' name='colour' checked></td>
                                            <td><input type='checkbox' name='colour'></td>
                                            <td><input type='checkbox' name='colour'></td>
                                            <td><input type='checkbox' name='colour'></td>
                                        </tr>
                                        <tr>
                                            <td><p style='font-family: BMehrBold, Arial, Helvetica, sans-serif;'>$pro_one</p></td>
                                            <td><p style='font-family: BMehrBold, Arial, Helvetica, sans-serif;'>$pro_two</p></td>
                                            <td><p style='font-family: BMehrBold, Arial, Helvetica, sans-serif;'>$pro_three</p></td>
                                            <td><p style='font-family: BMehrBold, Arial, Helvetica, sans-serif;'>$pro_four</p></td>
                                        </tr>
                                    </table>
                                    <button type='button' class='btn btn-fefault cart'>
                                        <i class='fa fa-shopping-cart' ></i>
                                        Add to cart
                                    </button></br>
                                </form>";
?>

您可以使用 empty() 函数来检查变量是否为空

echo "
<form id='submittion'> 
<label style='font-family: BMehrBold, Arial, Helvetica, sans-serif;'>Quantity:</label>
<input type='text' value='1' /></br></br>
<table style='margin-bottom:10px;width: 100%;'>
    <tr>
        <td><input type='checkbox' name='colour' checked></td>
        <td><input type='checkbox' name='colour'></td>";
        <?php
        if(!empty($pro_three)){ echo "<td><input type='checkbox' name='colour'></td>\n"; }
        if(!empty($pro_four)){ echo "<td><input type='checkbox' name='colour'></td>\n"; }
        ?>
echo "
    </tr>
    <tr>
        <td><p style='font-family: BMehrBold, Arial, Helvetica, sans-serif;'>$pro_one</p></td>
        <td><p style='font-family: BMehrBold, Arial, Helvetica, sans-serif;'>$pro_two</p></td>
        <?php 
        if(!empty($pro_three)){ echo "<td><p style='font-family: BMehrBold, Arial, Helvetica, sans-serif;'>$pro_three</p></td>\n"; }
        if(!empty($pro_four)){ echo "<td><p style='font-family: BMehrBold, Arial, Helvetica, sans-serif;'>$pro_four</p></td>\n"; }
        ?>
    </tr>
</table>
<button type='button' class='btn btn-fefault cart'>
    <i class='fa fa-shopping-cart' ></i>
    Add to cart
</button></br>
</form>
";

我会建议以下解决方案:

<?php
 $colors[] = $row_pro['color_one'];
 $colors[] = $row_pro['color_two'];
 $colors[] = $row_pro['color_three'];
 $colors[] = $row_pro['color_four'];

 foreach($colors as $color) {
    $checked = "";
    if($color == "your color") {
      $checked = "checked";
    }      
    echo "<td><input type='checkbox' name='colour' $checked>$color</td>";   
 }

?>