0

我试图从我的数据库中对特定数据进行排序。我已经根据第一页的用户输入来显示特定数据。但是,当我尝试对表格进行排序时,出现了一些未定义变量的错误

这是我在表格中显示数据并对表格进行排序的代码

<?php
//connect to server
$connect = mysql_connect("localhost", "root", "") or die('no database');

//connect to database
//select the database
mysql_select_db("fak_databases");
//submit button
if($_POST['formSubmit'] == "Submit")
{
    $country = $_POST['country'];
}

//query the database
if($country == TRUE) {

    $order = "";
    $sort = "asc"; 

    if(isset($_GET['orderby'])){
        $order = $_GET['orderby']; 
        $sort = $_GET['sort'];  

        //limiting the possible values of order/sort variables
        if($order != 'wipo_applicant1_city' && $order != 'applicant1_addr1')$order = "applicant1_addr1";
            if($sort != 'asc' && $sort != 'desc')$sort = "asc";
                $sql = "SELECT wipo_applicant1_city, applicant1_addr1 FROM auip_wipo_sample WHERE applicant1_country='$country' ORDER BY ".mysql_real_escape_string($order)." ".$sort; 

                //here we reverse the sort variable
                if($sort == "asc"){
                    $sort = "desc";
                }
            else{
                $sort = "asc";
            }
        }       
} 
    $sql = "SELECT wipo_applicant1_city, applicant1_addr1 FROM auip_wipo_sample WHERE applicant1_country='$country'";
    $result = mysql_query($sql) or die('failed to run');
    $num_rows = mysql_num_rows($result);
    $row_counter = 0; 

    $icon = "";
    echo "<table  border=\"1\" cellspacing=\"0\">\n";
    echo "<tr>\n"; 

    // first column
    echo "<th>";
    $icon = "";
    if($order == "wipo_applicant1_city"){
        if($sort == "asc"){
            $icon = "<img src=\"images/up.png\" class=\"arrowSpace\"/>";
        }
        if($sort == "desc"){
            $icon = "<img src=\"images/down.png\" class=\"arrowSpace\"/>";
        }
    }

    //print the result
    echo "<a href='showDB1.php?orderby=wipo_applicant1_city&sort=".$sort."'>City</a>".$icon;
    echo "</th>\n";


    // second column
    echo "<th>";
    $icon = "";
    if($order == "applicant1_addr1"){
        if($sort == "asc"){
            $icon = "<img src=\"images/up.png\" class=\"arrowSpace\"/>";
        }
        if($sort == "desc"){
            $icon = "<img src=\"images/down.png\" class=\"arrowSpace\"/>";
        }
    }
    echo "<a href='showDB1.php?orderby=applicant1_addr1&sort=".$sort."'>Address</a>".$icon;
    echo "</th>\n";
    echo "</tr>";

//fetch the result

while($row = mysql_fetch_array($result))
{
    if($row_counter % 2){
            $row_color="bgcolor='#FFFFFF'";
        }else{
            $row_color="bgcolor='#F3F6F8'";
        }
    echo "<tr class=\"TrColor\" ".$row_color.">";
    echo "<td>" . $row['wipo_applicant1_city'] . "</td>\n";
    echo "<td>" . $row['applicant1_addr1'] . "</td>\n";
    echo "</tr>";
    $row_counter++;
}

Print "</table>";
?>

我有错误

 Undefined index: formSubmit in C:\xampp\htdocs\fak_ict1999\sorting\showDB1.php

 Undefined variable: country in C:\xampp\htdocs\fak_ict1999\sorting\showDB1.php

 Undefined variable: country in C:\xampp\htdocs\fak_ict1999\sorting\showDB1.php 

 Undefined variable: order in C:\xampp\htdocs\fak_ict1999\sorting\showDB1.php

 Undefined variable: sort in C:\xampp\htdocs\fak_ict1999\sorting\showDB1.php 

当我分析我的代码时,我认为我在

 if($_POST['formSubmit'] == "Submit")
{
    $country = $_POST['country'];
}

//query the database
if($country == TRUE) {

    $order = "";
    $sort = "asc"; 

    if(isset($_GET['orderby'])){
        $order = $_GET['orderby']; 
        $sort = $_GET['sort'];  

        //limiting the possible values of order/sort variables
        if($order != 'wipo_applicant1_city' && $order != 'applicant1_addr1')$order = "applicant1_addr1";
            if($sort != 'asc' && $sort != 'desc')$sort = "asc";
                $sql = "SELECT wipo_applicant1_city, applicant1_addr1 FROM auip_wipo_sample WHERE applicant1_country='$country' ORDER BY ".mysql_real_escape_string($order)." ".$sort; 

                //here we reverse the sort variable
                if($sort == "asc"){
                    $sort = "desc";
                }
            else{
                $sort = "asc";
            }
        }       
} 
    $sql = "SELECT wipo_applicant1_city, applicant1_addr1 FROM auip_wipo_sample WHERE applicant1_country='$country'";

因为当我尝试对表格进行排序时,排序功能正在尝试再次访问表单提交,这是表单提交仅在用户选择他们的选项时在第一页上访问。

有谁知道如何解决这个问题?

我的html代码

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Data Mining</title>
</head>

<body>
    <form action="showDB.php" method="post">
    <table border="0">
    <tr>
        <th colspan="3">test</th>
    </tr>
    <tr>
        <td>Select Foreign Agent Country</td>
        <td></td>
        <td>
        <select name="country">
        <option value="US">United States</option>
        <option value="NZ">New Zealand</option>
        <option value="JP">Japan</option>
        </select> 
        </td>
      </tr>
        <td colspan="3">
        <input type="submit" name="formSubmit" value="Submit">
        </td>
    </table>
    </form>

</body>
</html>

这是我的 javascript 代码

function SelectAll(btn) {
    var blnVal = false;
    if (btn.value == "Select All") {
        btn.value = "Unselect All";
        blnVal = true;
    }else {
        btn.value = "Select All";
        blnVal = false;
    }
    var d = document.forms["auip_wipo_sample"];
    if(d["auip_wipo_sample[]"] == null)
    {}
    else if (d["auip_wipo_sample[]"].length == null) {
        d["auip_wipo_sample[]"].checked = blnVal;
    }
    else {
        for (var i = 0; i < d["auip_wipo_sample[]"].length; i++) {
            d["auip_wipo_sample[]"][i].checked = blnVal;
        }
    }
}
4

2 回答 2

0

您的变量的问题是您忘记初始化 $country 并且您需要检查例如 isset() 或 empty() 如果 $_POST 全局值“formSubmit”可用。

这里的示例代码:

<?php

print_r($_POST);

$country = '';

if (isset($_POST['submitted']) && $_POST['submitted'] === 'submit')
{
    $country = 'Pangea';
}

print $country;

if ($country == true)
{
    print ' => country is true' . PHP_EOL;
}
else
{
    print ' => country is false' . PHP_EOL;
}

?>

<!doctype html>
<html>
    <head>
    </head>
    <body>
        <form id="myForm" action="test.php" method="post">
            <input type="submit" name="submitted" value="submit" />
        </form>
    </body>
</html>

希望这对您有所帮助。

于 2013-05-18T16:37:35.470 回答
0

通过您的代码,我看到了一些我试图解释为什么它们不起作用的东西:那么当然你不应该使用 mysql_* 函数,因为它们已被弃用。我不明白你对 js 函数的调用在哪里,所以我暂时忽略了这部分。

我希望这会有所帮助。

<?php
//connect to server
$connect = mysql_connect("localhost", "root", "") or die('no database');

//connect to database
//select the database
mysql_select_db("fak_databases");
//submit button
/* skip this part. Only relevant to check name of submit button when having more submit-buttons in same form
if($_POST['formSubmit'] == "Submit")
{
    $country = $_POST['country'];
}
*/

//query the database
//if($country == TRUE) { 
if (isset($_REQUEST['country']) { //do like this instead (because country can arrive from link or from form 
    $country = $_REQUEST['country'];

    $order = "";
    $sort = "asc"; 

    //if(isset($_GET['orderby'])){ change to:
    if(isset($_GET['orderby']) && isset($_GET['sort'])) {

        $order = $_GET['orderby']; 
        $sort = $_GET['sort'];  //You're getting the value from $_GET['sort'] but you never check if it is set (like you do with orderby)

        //limiting the possible values of order/sort variables
        if($order != 'wipo_applicant1_city' && $order != 'applicant1_addr1')$order = "applicant1_addr1";
            if($sort != 'asc' && $sort != 'desc')$sort = "asc";
                $sql = "SELECT wipo_applicant1_city, applicant1_addr1 FROM auip_wipo_sample WHERE applicant1_country='$country' ORDER BY ".mysql_real_escape_string($order)." ".$sort; 

                //here we reverse the sort variable
                if($sort == "asc"){
                    $sort = "desc";
                }
            else{
                $sort = "asc";
            }
        }       
//} End of check isset($_GET['country']) should be move down to assure that $country is set 


    $sql = "SELECT wipo_applicant1_city, applicant1_addr1 FROM auip_wipo_sample WHERE applicant1_country='$country'";
    $result = mysql_query($sql) or die('failed to run');
    $num_rows = mysql_num_rows($result);
    $row_counter = 0; 

    $icon = "";
    echo "<table  border=\"1\" cellspacing=\"0\">\n";
    echo "<tr>\n"; 

    // first column
    echo "<th>";
    $icon = "";
    if($order == "wipo_applicant1_city"){
        if($sort == "asc"){
            $icon = "<img src=\"images/up.png\" class=\"arrowSpace\"/>";
        }
        if($sort == "desc"){
            $icon = "<img src=\"images/down.png\" class=\"arrowSpace\"/>";
        }
    }

    //print the result
    //echo "<a href='showDB1.php?orderby=wipo_applicant1_city&sort=".$sort."'>City</a>".$icon; //orderby and sort is defined but not $_POST['country']
    echo "<a href='showDB1.php?country=".$country."&orderby=wipo_applicant1_city&sort=".$sort."'>City</a>".$icon; //change to this instead so country is passed

    echo "</th>\n";


    // second column
    echo "<th>";
    $icon = "";
    if($order == "applicant1_addr1"){
        if($sort == "asc"){
            $icon = "<img src=\"images/up.png\" class=\"arrowSpace\"/>";
        }
        if($sort == "desc"){
            $icon = "<img src=\"images/down.png\" class=\"arrowSpace\"/>";
        }
    }
    //echo "<a href='showDB1.php?orderby=applicant1_addr1&sort=".$sort."'>Address</a>".$icon; //orderby and sort is defined but not $_POST['country']
    echo "<a href='showDB1.php?country=".$country."&orderby=applicant1_addr1&sort=".$sort."'>Address</a>".$icon; //change to this instead so country is passed
    echo "</th>\n";
    echo "</tr>";

//fetch the result

while($row = mysql_fetch_array($result))
{
    if($row_counter % 2){
            $row_color="bgcolor='#FFFFFF'";
        }else{
            $row_color="bgcolor='#F3F6F8'";
        }
    echo "<tr class=\"TrColor\" ".$row_color.">";
    echo "<td>" . $row['wipo_applicant1_city'] . "</td>\n";
    echo "<td>" . $row['applicant1_addr1'] . "</td>\n";
    echo "</tr>";
    $row_counter++;
}

Print "</table>";


} //End of check isset($_GET['country']) is moved to the end
?>
于 2013-05-19T22:33:10.717 回答