0

我在设计这个网站时遇到问题,在我睁大眼睛之前寻求帮助。以下脚本的预期结果是一个网页,其输入表单应如下所示(使用

James Craig | Week 7 | <INPUT>max</INPUT | <INPUT>ctrl</INPUT>
Jo Mama   | Week 7 | <INPUT>max</INPUT | <INPUT>ctrl</INPUT>

但是,我实际看到的是:

James Craig Week 7 max: ctrl: 

没有文本输入字段,也没有表格格式。此外,它没有显示第二人称。

然后:页面锁定并使浏览器崩溃。

我怀疑我的循环没有结束,但此时我已经迷失了。我知道有很多问题要问,但是有人可以在这里理顺我吗?:)

我正在使用的基本数据:

数据库样本数据

Table Name: loginUsers
--------------------------------------------------------
|      storeID      |      login     |    password     |
|-------------------|----------------|-----------------|
|       0460        |    store0460   |    store0460    |
--------------------------------------------------------

Table Name: specialistData
--------------------------------------------------------
|    specialistID   |    week    |     max    | ctrl   |
|------------------------------------------------------|
|         1         |      1     |    28.50   |  19.99 |
|         1         |      2     |    17.85   |  21.99 |
|         1         |      3     |    16.99   |  99.99 |
|         1         |      4     |    28.50   |  19.99 |
|         1         |      5     |    28.50   |  19.99 |
|         1         |      6     |    28.50   |  19.99 |
|         2         |      1     |    28.50   |  19.99 |
|         2         |      2     |    17.85   |  21.99 |
|         2         |      3     |    16.99   |  99.99 |
|         2         |      4     |    28.50   |  19.99 | 
|         2         |      5     |    28.50   |  19.99 |
|         2         |      6     |    28.50   |  19.99 |
--------------------------------------------------------    
Table Name: usersSpecialists
--------------------------------------------------------
|      storeID      |  specialistID  |       name      |
|-------------------|----------------|-----------------|
|       0460        |        1       |    James Craig  |
|       0460        |        2       |     Jo Mama     |
--------------------------------------------------------

有问题的 PHP 文档(我删除了 DB 连接字符串,但我向您保证它们工作正常)

<?php

//Initialize the session
session_start();

//Check for existing session.  Route to login if not
if(!isset( $_SESSION['username'])){
header('Location: ../index.php');
}
?>

<?php include(dirname(__FILE__)."/functions.php"); ?>




<html>
<head>
    <title>Weekly Data</title>
    <link href="portal.css" rel="stylesheet">
</head>
<body>

        <div id="masthead">
            <ul class="nav nav-pills pull-right">
                <li class = "active"><a href="#">View Report</a></li>
                <li class = "active"><a href="#">Edit Specialists</a></li>
                <li class = "active"><a href="logout.php">Logout</a></li>
            </ul>
            <h3 class="muted">Sales Specialist Ranker - Weekly Data</h3>
        </div>

<hr>

<div id="jumbotron"><center>
            <h1>Enter Weekly Data</h1>
            <form action="submitData.php method="GET">

            </form>



            <a class="btn btn-large btn-success" href="#">Submit</a>        
</center></div>
<?php 

//This will determine the current week, compare it to the last entry for each specialist, and create a form to request any needed data.

//Obtain currently logged in store (Store Number)
    $currentStore = $_SESSION['username'];

//Obtain Current Week
    $currentWeek = date("W"); 

//Obtain list of specialists (specialist ID)
    //Create temp mysqli object
    $specialistList = new mysqli();

    //Grab specialist list
    $specialistList = getSpecialists($currentStore);

//Loop through each specialist and generate form

    //Start Form
    echo "<form name='specialistData' action='submitData.php' method='get'>";
    //Start Loop
        while($row = $specialistList->fetch_array(MYSQLI_ASSOC)){

            //Start HTML Table
            echo "<table class='dataEntry'>";

            //create temp mysqli object
            $lastWeek = new mysqli();

            //Get last entry for specialist
            $lastWeekData = lastEntry($row['specialistID']);
            $lastWeekData = $lastWeekData->fetch_array(MYSQLI_ASSOC);
            $lastWeek = $lastWeekData['week'];
            $specialistID = $row['specialistID'];
            $specialistName = $lastWeekData['name'];

            //compare last week to current week

                //Begin loop
                while($lastWeek < $currentWeek){

                    //create HTML Input for each specialist
                    echo    "<tr>\n
                                <td>$specialistName</td>\n
                                <td>Week: $lastWeek</td>\n
                                <td>Max: <input type='text' name='".$specialistID."-".$lastWeek."-max /></td>\n
                                <td>Max: <input type='text' name='".$specialistID."-".$lastWeek."-ctrl /></td>\n
                            </tr>\n";

                    $lastweek++;
                }

            //Close HTML table for this specialist
            echo "</table>";
        }

        //Close HTML Form
        echo "</form>";

?>

<hr>
<div class="footer">
    <p> Footer Here</p>
</div>

</body>
</html>

这是我的functions.php

<?php //FUNCTIONS
function getSpecialists($user){ 
/*
        PURPOSE:   GET LIST OF SPECIALISTS THAT BELONG TO CURRENT USER
        ARGUMENTS: $user{USER ID - STRING}
        RETURN:    MYSQLI OBJECT CONTAINING RESULT LIST {specialistID}
*/

//Include DB Strings
$DB_HOST = "****";
$DB_NAME = "****";
$DB_USER = "****";
$DB_PASS = "****";

    //Query create the SQL object
    $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
    //Plan for error
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n". mysqli_connect_error(error)." \n WHEN: OBTAINING SPECIALIST LIST");
        exit();
    }

    //Create QUERY to return {specialistID}
    $sql = "SELECT      usersSpecialists.specialistID
            FROM        usersSpecialists
            INNER JOIN  loginUsers
            ON          usersSpecialists.storeID = loginUsers.storeID
            WHERE       loginUsers.login = '$user'";


    //Launch the query
    $specialistResults = $mysqli->query($sql);

    //Send back results of query
    return $specialistResults;  
}



function lastEntry($specialist){
/*
        PURPOSE:   GET THE LATEST ENTRY (WEEK NUMBER) FOR GIVEN SPECIALIST
        ARGUMENTS: $specialistID{SPECIALIST IN QUESTION - INT}
        RETURN:    MYSQLI OBJECT CONTAINING RESULT LIST {week, name}
*/
//Include DB connection strings
$DB_HOST = "****";
$DB_NAME = "****";
$DB_USER = "****";
$DB_PASS = "****!";

    //Query create the SQL object
    $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
    //Plan for error
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n". mysqli_connect_error(error)." \n WHEN: OBTAINING SPECIALIST LIST");
        exit();
    }

    //Create SQL query
    $sql = "SELECT      specialistData.week, usersSpecialists.name
            FROM        specialistData
            INNER JOIN  usersSpecialists
            ON          specialistData.specialistID = usersSpecialists.specialistID
            WHERE       specialistData.specialistID = $specialist
            ORDER BY    specialistData.week DESC
            LIMIT       1";

    //Launch SQL query
    $lastWeek = $mysqli->query($sql);

    //Return mysqli result $lastWeek {week, name}
    return $lastWeek;
}




?>
4

1 回答 1

0

我想出了一个解决办法。问题是由循环查询引起的。显然这使一切都超载了。我回到第一方,然后:

1) 创建了一个新查询,通过 JOINS 一次获得所有我需要的信息

2) 重写了我的 PHP,因此它循环返回的数据,无需进一步调用 mysqli->query

这是修改后的代码:

<?php

//Initialize the session
session_start();

//Check for existing session.  Route to login if not
if(!isset( $_SESSION['username'])){
header('Location: ../index.php');
}
?>





<?php  //This will determine the last week entered for each specialist and generate an entry form for all missing data

//Obtain currently logged in store (Store Number)
    $currentStore = $_SESSION['username'];

//Obtain Current Week
    $currentWeek = date("W"); 

//Include DB Strings
$DB_HOST = "****";
$DB_NAME = "****";
$DB_USER = "****";
$DB_PASS = "****";

//Query create the SQL object
    $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
//Plan for error
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n". mysqli_connect_error(error));
        exit();
    }

//Create QUERY to return {specialistID}
    $sql = "SELECT      usersSpecialists.specialistID, 
                        usersSpecialists.name, 
                        max(specialistData.week)
            FROM        loginUsers
            INNER JOIN  usersSpecialists    
            ON          loginUsers.storeID = usersSpecialists.storeID
            INNER JOIN  specialistData      
            ON          usersSpecialists.specialistID = specialistData.specialistID
            WHERE       loginUsers.login = '$currentStore'
            GROUP BY    usersSpecialists.specialistID, usersSpecialists.name";

//Launch query
    $results = $mysqli->query($sql);

//Get current week
    $currentWeek = date("W");  

//Loop through each specialist and generate form

    //Start Form
    echo "<form name='specialistData' action='submitData.php' method='get'>";

    //Start Loop
        while($row = $results->fetch_array(MYSQLI_ASSOC)){

            //Create visually friendly variables
            $name = $row['name'];
            $id = $row['specialistID'];
            $week = $row['max(specialistData.week)']; 

            //++ is there to negate the week that is already entered (The last week we have)
            $week++;

            //Check if data is current
            if($week < $currentWeek){

                //Start Table for unique specialist
                echo "<table><th>Name</th><th>Week</th><th>Max</th><th></th>";

                //Loop until week matches for data entry
                while($week < $currentWeek){

                    //Create entries for each needed week
                    echo "<tr>
                            <td>$name</td>
                            <td>$week</td>
                            <td><input type='text' name='max" . $id . $week . "'></td>
                            <td><input type='text' name='ctrl" . $id . $week . "'></td>
                          </tr>";

                        //Increment $week
                        $week++;
                }
            } else {
                echo "<p>No data needed for ". $name . ".";
            }
        }

?>

谢谢大家!

于 2013-03-08T13:04:28.970 回答