php - 如果字段值=预设值,如何使用 PHP 打印 MySQL 表中的记录

标签 php mysql html-table

如果字段的值 = 多个预先声明的值之一,我想过滤 MySQL 表中显示的记录。我已经在表上运行查询,该查询根据用户从下拉菜单中选择的选项显示记录。但是,在页面刚刚加载时,用户从下拉框中选择一个选项之前,我希望表格仅显示一些记录。

<?php
require_once("includes/session.php");
include_once("includes/masterinclude.php");

$preferences = getPreferences();
$category = "";
$attribute1 = ""; $attribute2 = ""; $attriute3 = ""; $attribute4 = "";
$top_level="0";

$name = $_GET['member'];
$name = $_GET['countries'];

$information = getInformationPage($name);
$infopagename=$information->IN_NAME;
//meta data for information pages now taken from the information table
$pageTitle = $information->IN_NAME . html_entity_decode($information->IN_TITLE);
$pageMetaDescription = html_entity_decode($information->IN_META_DESC);
$pageMetaKeywords = html_entity_decode($information->IN_META_KEYWORDS);
$pageCustomHead = html_entity_decode($information->IN_CUSTOM_HEAD, ENT_QUOTES);

//initialise screen fields
$selected_member = "";
$id = "";
$username = ""; $username_original = "";
$password = ""; $password_original = "";
$password_test = "";
$title = "MR"; $first_name = ""; $last_name = ""; $company_name = "";
$address1 = ""; $address2 = ""; $town = ""; $county = ""; $country = ""; $postcode = ""; $phone = ""; $mobile = ""; $email = "";
$member_confirmed = "N";
$ast_first = 0; $ast_last = 0; $ast_company = 0; $ast_add1 = 0; $ast_add2 = 0; $ast_town = 0; $ast_county = 0; $ast_country = 0; $ast_post = 0; $ast_phone = 0;
$ast_mobile = 0; $ast_email = 0;
$ast_user = 0; $ast_pass = 0; $ast_passconf = 0;

$selected_product = "";
$members = Get_All_Members("ALL");
$counties = Get_All_Counties("ALL");
$_GET['searchdata'] = $_POST['SEARCH_DATA']; $_GET['searchmember'] = $_POST['MEMBER'];

$selected_county = $counties->CTY_COUNTY;
$_GET['searchdata'] = $_POST['SEARCH_DATA']; $_GET['searchcounty'] = $_POST['COUNTY'];

$selected_country = $countries->CTY_COUNTRY;
$_GET['searchdata'] = $_POST['SEARCH_DATA']; $_GET['searchcountry'] = $_POST['COUNTRY'];

include_once("includes/header.php");
?>

<!-- start: Page header / Breadcrumbs -->

<div id="breadcrumbs">
    <div class="container">
        <div class="breadcrumbs"> 
            <a href="/">Home</a><i class="icon-angle-right"></i>Export Stockists
        </div>
    </div>
</div>

<!-- end: Page header / Breadcrumbs -->


<!-- start: Container -->
<div id="container">
    <div class="container">
        <div class="row-fluid">

        <!-- start: Page section -->
        <section class="span12">
            <div class="row-fluid shop-result">
                <div class="inner darken clearfix">
                    <h1>Export Stockists</h1>
                </div>
            </div>

            <div class="row-fluid">

            <?php
            $sql = "SELECT * FROM member ";

            if (isset($_POST['search'])) {

                $search_term = mysql_real_escape_string($_POST['search_box']);

                $sql .= "WHERE MB_COUNTRY = '{$search_term}' ";

                $sql .=" OR MB_COMPANY = '{$search_term}' ";
            }
            $query = mysql_query($sql) or die(mysql_error());
            ?>

            <form name="search_form" method="POST" action="stockists_country.php" enctype="multipart/form-data" class="form-search">
                <div class="input-append">
                    <select name="all_countries" onchange="MM_jumpMenu('parent',this,1)" type="submit"> 
                        <option disabled="disabled" selected="selected" hidden="hidden">Select County</option>
                        <option value="103">Republic of Ireland</option>
                        <option value="39">Canada</option>
                        <option value="149">Netherlands</option>
                        <option value="193">South Africa</option>
                        <option value="194">Spain</option>
                        <option value="228">United States</option>
                    </select>
                    <input type="submit" name="search" value="Search for Stockist" class="btn btn-primary">
                </div>
            </form>

            <?php
            $sql = "SELECT * FROM member ";

            if (isset($_POST['search'])) {

                $search_term = mysql_real_escape_string($_POST['all_counties']);

                $sql .= "WHERE MB_COUNTRY = '{$search_term}' ";

                //$sql .= "WHERE MB_COUNTRY IN ('103','39','149','193','194','228')";

                $sql .=" OR MB_COMPANY = '{$search_term}' ";
            }
            $query2 = mysql_query($sql) or die(mysql_error());
            ?>

            <table width="70%" cellpadding="5" cellspace="5" class="table table-hover table-striped">

            <tr>
                <td><strong>Company Name</strong></td>
                <td><strong>Website</strong></td>
                <td><strong>Phone</strong></td>
                <td><strong>Address</strong></td>
            </tr>

            <?php
            $a = "MB_COUNTRY";
            $b = "103";
            $c = "39";
            $d = "149";
            $e = "193";
            $f = "194";
            $g = "228";

            if( in_array($a, array($b,$c,$d,$e,$f,$g)) ){
            ?>

            <?php  while ($row = mysql_fetch_array($query)) { ?>
            <tr> 
                <td><?php echo $row['MB_COMPANY'];?></td>
                <td><a href="http://<?php echo $row['MB_MOBILE'];?>"><?php echo $row['MB_MOBILE'];?></a></td>
                <td><?php echo $row['MB_PHONE'];?></td>
                <td><?php echo $row['MB_ADDRESS1'];?>, <?php echo $row['MB_ADDRESS2'];?>, <?php echo $row['MB_TOWN'];?>, <?php echo $row['MB_COUNTY'];?></td>
            </tr>
            <?php } ?>
            <?php } ?>



            </table>

            <a class="btn btn-primary" href="login_member.php">Member Login</a>

        </section>

        <!-- end: Page section -->

        </div>

    </div>
</div>

<!-- end: Container -->

<?php
  include_once("includes/footer.php");
?>

我尝试在我的 table 周围放置一个 IF ,表示 if MB_COUTRY = $b,$c,$d,$e,$f,$g then print记录。但是,当使用它时,我的表没有显示任何记录,但没有错误。 我想在同一字段 (MB_COUNTRY) 但在不同时间运行这两个查询。 如果有人知道有帮助的查询以及如何在正确的时间运行它们,那将是一个巨大的帮助。

最佳答案

您需要对每一行执行 if 才能使其正常工作。此外,您需要测试 $row 中的值而不是任意值。您的 if 将需要另一条指令来测试 user_search 以确保它尚未被过滤。

<?php
$a = "MB_COUNTRY";
$b = "103";
$c = "39";
$d = "149";
$e = "193";
$f = "194";
$g = "228";

while ($row = mysql_fetch_array($query)) { 
if( in_array($row[$a], array($b,$c,$d,$e,$f,$g)) ){?>

            <tr> 
                <td><?php echo $row['MB_COMPANY'];?></td>
                <td><a href="http://<?php echo $row['MB_MOBILE'];?>"><?php echo $row['MB_MOBILE'];?></a></td>
                <td><?php echo $row['MB_PHONE'];?></td>
                <td><?php echo $row['MB_ADDRESS1'];?>, <?php echo $row['MB_ADDRESS2'];?>, <?php echo $row['MB_TOWN'];?>, <?php echo $row['MB_COUNTY'];?></td>
            </tr>
<?php } ?>
<?php } ?>

关于php - 如果字段值=预设值,如何使用 PHP 打印 MySQL 表中的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28960733/

相关文章:

mysql - 优化查询以通过内连接在 MySQL 中查找平均值

mysql - 无法从 MySQL 检索日期

html - 如何使 <td> 适合内容

php - 尝试安装 mysql php 扩展时 Docker run 命令失败

php - 数据未插入到 MySQL 数据库

php - 连接具有相同 ID 和相同列名但值不同的两个表

html - 什么时候使用 DIV 而不是 TABLE 更合适,反之亦然?

php - 不带容器的Bitbucket管道

php while循环不正确地显示sql数据

html - 如何根据最宽的列使 HTML 表格中的所有列的列宽保持相等