0

what am i doing wrong here?

<?php
include 'connect.php';
$query = 'SELECT productionorder.Finished FROM productionorder WHERE productionorder.ProductionOrderId = '.$id;
$result = mysql_query($query) or die ('Error : ' . mysql_error());
while($data = mysql_fetch_array($result)){
$status = $data['Finished'];
}
if($status == 1){
header('location: index.php?user='.$user.'');
}
else{}
?>

the query returns 1 and is going to else statement
please help me out here..

5
Contributors
25
Replies
62
Views
4 Years
Discussion Span
Last Post by chemwile
Featured Replies
  • Are you actually in the position to put an header? When something is already returned an header cannot processed anymore and if the according errormessage is supressed it may seem the else-path is choosen. Read More

0

Are you sure the query returns 1? Test it with some code like:

if($status == 1) {
    die('Status is obviously 1');
    // header('location: index.php?user='.$user.'');
} else {
    die('Status is actually ' . print_r($status, 1));
}
0

Some questions that might help you:

  • Maybe $status returns a string '1'.
  • You write the query returns 1, but does $data['Finished'] also return 1.
  • What value does $status have outside the while-block?

Good luck

0

outside the while loop it returns 1.
tried it with quotes and without but i retrieve an integer from the database. so should not be a string.

$query = 'SELECT productionorder.Finished FROM productionorder WHERE productionorder.ProductionOrderId = '.$id;
$result = mysql_query($query) or die ('Error : ' . mysql_error());
while($data = mysql_fetch_array($result)){
$status = $data['Finished'];
}
if($status == 1){
die('Status = 1');
//header('location: index.php?user='.$user.'');
}
else{
die('Status = ' . print_r($status, 1));
}

returns:
"Status = 1"

so if i see this correctly i have to print somethign for the value to become 1 and not stay empty or whatever.

0

so if i see this correctly i have to print somethign for the value to become 1 and not stay empty or whatever.

No, this was just a test. It might be that the location header is not getting the correct address or maybe some output has been sent before it (which prevents header for being sent). Remove the above debug code and try this:

if($status == 1) {
    die('location: index.php?user='.$user.'');
    //header('location: index.php?user='.$user.'');
} else {}

and check if the correct location string has been constructed and displayed.

Edited by broj1

1

Are you actually in the position to put an header? When something is already returned an header cannot processed anymore and if the according errormessage is supressed it may seem the else-path is choosen.

0

that returns location: index.php?user=123
just as expected since i'm logging in under user id 123

0

basicly the script test for a number of things before starting the script "start production" so it has some checks before and everytime 1 of those fail it should go to index.php?user=x
else it should do nothing. if i remove this it will go to a header on the next check anyway.

0

Then let's try if headers have already been sent:

if(headers_sent()) {
    die('Headers have already been sent, you can not send them again.');
}

if($status == 1) {
die('location: index.php?user='.$user.'');
//header('location: index.php?user='.$user.'');
} else {}

Edited by broj1

0

updated the code and returns nothing
just a complete blanc screen.

seems like a typo or something.

Edited by thijscream

0

headers_send() can;'t use the ()
and indeed i get the result "headers have already beens send cannor send again"

checked the code and header send is always in a if statement and always to index.php?user=x
so how do i not get to that page?
possible it has to do with nginx running??

Edited by thijscream

0

You make sure no HTML output has been sent before any header() function. You can achieve that in several ways:

  1. make sure no html is before header() functions
  2. make sure no single space (or whitespace chars) is outside <?php ?> tags before header() functions
  3. make sure no HTML output or white space is in included files function before header() functions

If sending an output before header() functions can not be avoided, use output buffering.

Edited by broj1

0

I met this message after I edited my code file with Visual Studio that added some invisible characters for ANSI format (or something alike) in the start of the file. When these were returned, the headers couldn't be send anymore.

0

run true all include scripts. and all it is returning in source code is "headers have already beens send, cannot send again".

0

So can you remove the output before the header function? If not sure, post the scripts.

0

here is the complete script, not just a piece of it:

<?php
//database connection
include '../../include/connect.php';
include '../../include/function.php';
if(isset($_REQUEST['i'])){
                $i = $_REQUEST['i'];
                if($i == 1){
                //budget
                               $user = $_REQUEST['budgettimeuser'];
                               $id = $_REQUEST['budgettimeid'];
                               $query = 'SELECT count(budgettime.BudgetTimeId) as counter FROM budgettime WHERE budgettime.PersonId = '.$user.' AND budgettime.Finished IS NULL';
                               $result = mysql_query($query) or die ('Error budget 1: ' . mysql_error());
                               while($data = mysql_fetch_array($result)){
                                               if($data['counter'] == 0){
                                               //niks aan de hand
                                               $time = date("Y-m-d G:i:s");
                                               $query = 'INSERT INTO `budgettime` (`PersonId`, `Start`, `BudgetId`) VALUES('.$user.', "'.$time.'", '.$id.')';
                                               mysql_query($query) or die ('Error budget 2: ' . mysql_error());
                                               header('location:budget.php?user='.$user.'&id='.$id);
                                               }
                                               else{
                                               //ga naar activiteit.
                                               //f = 0 (message activity is not finished)
                                                               $query = 'SELECT budgettime.BudgetTimeId FROM budgettime WHERE budgettime.PersonId = '.$user.' AND budgettime.Finished IS NULL LIMIT 1';
                                                               $result = mysql_query($query) or die ('Error budget 3 : ' . mysql_error());
                                                               while($data = mysql_fetch_array($result)){
                                                                              header('location:budget.php?f=0&budgettimeid='.$data['BudgetTimeId']);
                                                               }
                                               }
                               }
                }             
                elseif($i == 2){
                               if(isset($_REQUEST['productiontimeid'])){
                                               //production
                                               $user = $_REQUEST['productiontimeuser'];
                                               $id = $_REQUEST['productiontimeid'];
                                               if($id == ''){
                                                               header('location:index.php?user='.$user.'');
                                               }
                                               else{}
                                               $disableorder = DisableOrder($id);
                                               $query = 'SELECT productionorder.Finished FROM productionorder WHERE productionorder.ProductionOrderId = '.$id;
                                               $result = mysql_query($query) or die ('Error : ' . mysql_error());
                                               while($data = mysql_fetch_array($result)){
                                                               $status = $data['Finished'];
                                               }

                                               if(headers_send){
                                                               die('headers have already been send, cannot send again');
                                               }
                                               else{}
                                               if($status == 1){
                                                               die('location: index.php?user='.$user.'');
                                               }
                                               else{

                                               }
                                               if($disableorder >= 2){
                                                               header('location: index.php?user='.$user.'');
                                                               echo 'header';
                                                               echo $disableorder;
                                               }
                                               elseif($disableorder <= 1){
                                                               echo $disableorder;
                                                                              $id = $_REQUEST['productiontimeid'];
                                                                              $query = 'SELECT productionorder.ProductionOrderId FROM productionorder WHERE productionorder.ProductionOrderId = '.$id;
                                                                              echo $query;
                                                                              echo '<br>';
                                                                              $result = mysql_query($query) or die ('Error : ' . mysql_error());
                                                                              if(mysql_num_rows($result) > 0){
                                                                                              //check if user excists.
                                                                                              if(isset($_REQUEST['productiontimeuser'])){
                                                                                                              $user = $_REQUEST['productiontimeuser'];
                                                                                                              $query = 'SELECT people.PersonId FROM people WHERE people.PersonId = '.$user;
                                                                                                              echo $query;
                                                                                                              echo '<br>';
                                                                                                              $result = mysql_query($query) or die ('Error : ' . mysql_error());
                                                                                                              if(mysql_num_rows($result) == 1){

                                                                                                              }
                                                                                                              else{
                                                                                                                             header('location:index.php?user='.$user.'');
                                                                                                              }
                                                                                              }
                                                                              }
                                                                              else
                                                                              {
                                                                                              header('location:index.php?user='.$user.'');
                                                                              }


                                                               $user = $_REQUEST['productiontimeuser'];
                                                               $id = $_REQUEST['productiontimeid'];
                                                               $query = 'SELECT count(productiontime.ProductionOrderId) as counter FROM productiontime WHERE productiontime.PersonId = '.$user.' AND productiontime.Finished IS NULL';
                                                               $result = mysql_query($query) or die ('Error : ' . mysql_error());
                                                               while($data = mysql_fetch_array($result)){
                                                                              if($data['counter'] == 0){
                                                                              //niks aan de hand
                                                                              $time = date("Y-m-d G:i:s");
                                                                              $query = 'SELECT `NumberOfItems`, `ProductionOrderId` FROM `productionorder` WHERE ProductionOrderId = '.$id;
                                                                              $result = mysql_query($query) or die ('Error : ' . mysql_error());
                                                                              while($data = mysql_fetch_array($result)){
                                                                                              $numberofitems = $data['NumberOfItems'];
                                                                                              $productionorderid = $data['ProductionOrderId'];
                                                                              }
                                                                              $query = 'INSERT INTO `productiontime` (`ProductionOrderId`, `PersonId`, `Start`, `Amount`) VALUES('.$productionorderid.', '.$user.', "'.$time.'", '.$numberofitems.')';
                                                                              mysql_query($query) or die ('Error : ' . mysql_error());
                                                                              header('location:production.php?productionorderuser='.$user.'&productionorderid='.$id);
                                                                              }
                                                                              else{
                                                                              //ga naar activiteit.
                                                                              //f = 0 (message activity is not finished)
                                                                                              $query = 'SELECT productiontime.ProductionOrderId FROM productiontime WHERE productiontime.PersonId = '.$user.' AND productiontime.Finished IS NULL LIMIT 1';
                                                                                              $result = mysql_query($query) or die ('Error : ' . mysql_error());
                                                                                              while($data = mysql_fetch_array($result)){
                                                                                                              header('location:production.php?f=0&productionorderid='.$data['ProductionOrderId'].'&productionorderuser='.$user);
                                                                                              }
                                                                              }
                                                               }
                                               }
                               }
                               else{
                                               header('location:index.php?user='.$user.'');
                               }
                }
                elseif($i == 3){
                               //non production
                               $user = $_REQUEST['nonproductiontimeuser'];
                               $id = $_REQUEST['nonproductiontimeid'];
                               $query = 'SELECT count(nonproductiontime.NonProductionTimeId) as counter FROM nonproductiontime WHERE nonproductiontime.PersonId = '.$user.' AND nonproductiontime.Finished IS NULL';
                               $result = mysql_query($query) or die ('Error : ' . mysql_error());
                               while($data = mysql_fetch_array($result)){
                                               if($data['counter'] == 0){
                                               //niks aan de hand
                                               $time = date("Y-m-d G:i:s");
                                               $query = 'INSERT INTO `nonproductiontime` (`PersonId`, `Start`, `Activity`) VALUES('.$user.', "'.$time.'", '.$id.')';
                                               mysql_query($query) or die ('Error : ' . mysql_error());
                                               header('location:nonproduction.php?user='.$user.'&id='.$id);
                                               }
                                               else{
                                               //ga naar activiteit.
                                               //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                                               //moet ik nog maken
                                                               $query = 'SELECT nonproductiontime.NonProductionTimeId FROM nonproductiontime WHERE nonproductiontime.PersonId = '.$user.' AND nonproductiontime.Finished IS NULL LIMIT 1';
                                                               $result = mysql_query($query) or die ('Error : ' . mysql_error());
                                                               while($data = mysql_fetch_array($result)){
                                                                              header('location:nonproduction.php?f=0&nonproductiontimeid='.$data['NonProductionTimeId']);
                                                               }
                                               }
                               }
                }
                else{
                               header('location:index.php?user='.$user.'');
                }             
}
else{
                header('location:index.php?user='.$user.'');
}
?>
0

First remove all the echo statements in the code since they send output too early. Some of them are not needed at all (since they are positioned after the redirection should happen). If this does not work check teh included scripts (or post them here). If you post the database connection script here, do not forget to remove password and other confidential data.

0

here is connect.php

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password='lalala123'; // Mysql password
$db_name="system"; // Database name 

// Connect to server and select databse.
$n = 0;
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
0

(headers_send()) wasn't working for me.
gave an error(white page)
and i think you mean
if(headers_send()) instead of if((headers_send())

0

function.php calls like 20 functions in different pages. but i don't use any of those functions so can't be it

0

Broj1 said:

if((headers_sent())

Not as you've said:

if(headers_send())

with or without the extra parentheses.

0

(headers_send()) wasn't working for me.
gave an error(white page)

If you only get a blank page when in error then first turn on error reporting and display of errors in php.ini. This would have helped in first place.

0

thanks for help broj1, i found out what the problem was.. the problem was i didn't define the timezone in the script before calling date()
this caused to output an error and didn't display it but this was the text before the header. now working.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.