Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Bill Dowd
Bill Dowd
7,681 Points

PHP Warning: Cannot modify header information - with results from MySql, 1 result verses 1+ results

In my Web app, the results of a person search vary depending upon whether the result is 1 or many. If many, then the output is displaying a multi-column, hyperlinked list. If the result is only 1 then it should go directly to the dashboard for that person.

The function was originally designed to display the list, but I decided it would be faster, if there was only one result, why require the user to click on the only name in the list. So I used the header('Location: payees.php?action=dp&id=' . $payee_id); in the function. The problem is the page has already been partially rendered showing the heading, menu bar and search box. So I get the PHP Warning: Cannot modify header information.

The search term is sent to an included functions file in the "payees.php" page and is supposed to print the results, but in the function, I modified it, adding a conditional to use the header('Location') if there is only one result.

Payees.php file:

if(isset($_REQUEST['submit']) && $_REQUEST['submit'] == 'Search') {
    $searchterm = $_REQUEST['searchterm'];
    $querystring = "SELECT id, first, last, suspend, inactive FROM payee WHERE (first LIKE '" . $searchterm . "%' OR last LIKE '" . $searchterm . "%') ORDER BY last, first";
        print SearchPayees($querystring, $searchterm, $pagename); // functions.php

functions.php file:

function SearchPayees($searchstring, $searchterm, $pagename) {
    global $payee_col_count;
    $sql = new SQL();
    $sql->Query($searchstring);

    if($sql->GetNumRows() == 1) { //The condition to take you to the Payee Dashboard rather than display a list if there is only one result.
        $payee_id = $sql->GetResult(0, 'id');
        switch($pagename) {
            case 'payees.php':
                header('Location: payees.php?action=dp&id=' . $payee_id);
                break;
            case 'bills.php':
                header('Location: bills.php?action=dpb&payee_id=' . $payee_id);
                break;
            case 'reports.php':
                header('Location: reports.php?action=dpr&payee_id=' . $payee_id);
                break;
            case 'checks.php':
                header('Location: checks.php?action=dpc&payee_id=' . $payee_id);
                break;
        }
        exit();
    } else if($sql->GetNumRows() > 1) {
        switch($pagename) {
            case 'payees.php':
                $result = FormatPayeeList($sql->GetTableHash(), $payee_col_count, 15);
                break;
            case 'bills.php':
                $result = FormatBillsList($sql->GetTableHash());
                break;
            case 'reports.php':
                $result = FormatReportsList($sql->GetTableHash());
                break;
            case 'checks.php':
                $result = FormatChecksList($sql->GetTableHash());
                break;
        }
//      $result = FormatPayeeList($sql->GetTableHash());
        return $result;
    } else {
        return "<div class='norecords'>No Records Found for the search: \"" . $searchterm . "\"</div>\n";
    }

Is there someone who likes a challenge and would be willing to help me solve this? My beta tester is very used to the benefit of a single result since the WAMP 2.4 installation I have her on is allowing the error to be ignored. As I begin the process of moving it out to a Web host, I'm finding this to be a problem that must be addressed.

Thanks, Bill

Hey Bill, It is just a warning you can turn it off on web

 error_reporting(0);

3 Answers

Bill Dowd
Bill Dowd
7,681 Points

Hi had to swap out all the header(Location'...'); with Javascript: "?><script> window.location.href="payees.php?action=dp&id=<?=$payee_id?>"; </script><?php" or various versions of the id=. This worked well, but I'm bummed that I was unable to accomplish this using PHP.

Thanks, Bill

Bill Dowd
Bill Dowd
7,681 Points

Thanks. I tried that and it did stop the error reporting, but it's still not sending a single result to the dashboard. Any other thoughts?

Yes, I can but i need files to execute code myself and for tracking the error.

Bill Dowd
Bill Dowd
7,681 Points

This is a complicated application with a lot of files and a MySql DB. I can send you the payees.php file and I've already included the function used by that file. Will you be able to improvise with that?