【问题标题】:Populate a table with data from MySQL table PHP使用 MySQL 表 PHP 中的数据填充表
【发布时间】:2017-07-17 00:44:13
【问题描述】:

我有一个搜索框和一个下拉框。我正在使用这些从 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['counties'];
$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'];
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>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>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_COUNTY = '{$search_term}' ";
$sql .=" OR MB_COMPANY = '{$search_term}' ";
}
$query = mysql_query($sql) or die(mysql_error());
?>
<form name="search_form" method="POST" action="stockists.php">
<div class="row-fluid">
Search <input type="text" name="search_box" value=""/>
<input type="submit" name="search" value="Search the stockists" class="btn btn-primary">
</div>
</form>
<form name="search_form" method="POST" action="stockists.php" enctype="multipart/form-data">
<select name="all_counties" onchange="MM_jumpMenu('parent',this,1)" type="submit">
<?php
$sql = mysql_query("SELECT CTY_COUNTY FROM counties");
while ($row = mysql_fetch_array($sql)){
echo "<option value=\"stockists.php\">" . $row['CTY_COUNTY'] . "</option>";
}
?>
</select>
</form>
<?php
$sql = "SELECT * FROM member ";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['all_counties']);
$sql .= "WHERE MB_COUNTY = '{$search_term}' ";
$sql .=" OR MB_COMPANY = '{$search_term}' ";
}
$query2 = mysql_query($sql) or die(mysql_error());
?>
<table width="70%" cellpadding="5" cellspace="5">
<tr>
<td><strong>Company Name</strong></td>
<td><strong>Website</strong></td>
<td><strong>Phone</strong></td>
<td><strong>Address</strong></td>
</tr>
<?php while ($row = mysql_fetch_array($query)) {?>
<tr>
<td><?php echo $row['MB_COMPANY'];?></td>
<td><?php echo $row['MB_MOBILE'];?></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'];?>, <?php echo $row['MB_COUNTRY'];?></td>
</tr>
<?php } ?>
</table>
</section>
<!-- end: Page section -->
</div>
</div>
</div>
<!-- end: Container -->
<?php
include_once("includes/footer.php");
?>

【问题讨论】:

  • 停止使用 mysql_ 函数 - 它们已被弃用。使用mysqi_PDO 调用。还要研究存储过程,因为它们会使使用表单中的变量更容易、更安全
  • 放入 mysqi 只会破坏我所有的代码,所以这不是很有帮助。
  • @DaveyBoy 你说得对!! jonlloyd:简单地说,你将添加 mysqli 将不起作用,它只会在使用 mysqli 时抛出错误,你需要以正确的方式构建所有的东西,然后它才会起作用。希望这个技巧对你有用。
  • 请学会缩进,它让调试更容易。如果您希望得到帮助,那么以易于阅读的格式展示您的代码是很有礼貌的。
  • 我的代码按照粘贴的方式缩进。下面缩进↓ ↓ ↓

标签: php mysql database drop-down-menu html-table


【解决方案1】:
<?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['counties'];

$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'];

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>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>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_COUNTY = '{$search_term}' ";

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

            <form name="search_form" method="POST" action="stockists.php">
            <div class="row-fluid">
                Search <input type="text" name="search_box" value=""/>
                <input type="submit" name="search" value="Search the stockists" class="btn btn-primary">
            </div>
            </form>


            <form name="search_form" method="POST" action="stockists.php" enctype="multipart/form-data">
                <select name="all_counties" onchange="MM_jumpMenu('parent',this,1)" type="submit">
                    <?php 
                    $sql = mysqi_query("SELECT CTY_COUNTY FROM counties");
                    while ($row = mysql_fetch_array($sql)){
                    echo "<option value=\"stockists.php\">" . $row['CTY_COUNTY'] . "</option>";
                    }
                    ?>
                </select>
            </form>


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

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

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

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

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

            <table width="70%" cellpadding="5" cellspace="5">

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

            <?php while ($row = mysql_fetch_array($query)) {?>
            <tr>
                <td><?php echo $row['MB_COMPANY'];?></td>
                <td><?php echo $row['MB_MOBILE'];?></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'];?>, <?php echo $row['MB_COUNTRY'];?></td>
            </tr>
            <?php } ?>
            </table>

        </section>

        <!-- end: Page section -->

        </div>
    </div>
</div>

<!-- end: Container -->

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    • 2013-04-25
    • 1970-01-01
    相关资源
    最近更新 更多