Solved

Why dosent this php function works ?

Posted on 2013-06-17
12
206 Views
Last Modified: 2013-06-20
Hi!

Have this function:

function sjekk_om_flere_lokasjoner($lokasjon,$kundeid)
{
        $sql = "SELECT Lokasjons_id FROM Lokasjoner WHERE Lokasjons_id=$lokasjon and Kunde_id='$kundeid'";
        $res = mysql_query($sql);
        
        // IF THE QUERY SUCCEEDED
        if ($res)
        {
            // THERE SHOULD BE ONE ROW
            $num = mysql_num_rows($res);
            if ($num)
            {
                // Retuner 1 hvis finnes lokasjon
                return true;
          
            }
			else
			{
			    return false;
			}
        }
		else
		{
		  return false;
		}
}

Open in new window


I am using this function here:

<?php // RAY_EE_login.php
require_once('RAY_EE_config.php');
$table1 = 'Lokasjoner';
// ACCESS TO THIS PAGE IS TESTED BUT NOT CONTROLLED
if ($uid = access_control(TRUE))
{
   $brukerID = brukerident('xxx');
   $navn = $_GET['Navn'];
   $lokk = $_GET['lokk'];   
   $kundeid = kundeid();
   
   $finneslokasjon = sjekk_om_flere_lokasjoner($lokk,$kundeid);

   if ($finneslokasjon==false)
   {
        header("location: http://www.softkey.no/webtest/maiside.php");
	    exit;
   }
}
else
{
        header("location: http://www.softkey.no/webtest/xxx.php");
	    exit;
}
?>
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8" />
<title>Test system</title>

</head>

<body>
<link rel="stylesheet" href="default.css">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<table summary="Services, or Links box template" class="servicesT" cellspacing="1">
<tr><td colspan="2" class="header1" height="20"><a href="http://www.softkey.no/webtest/hovide.php" target="_self">
<img src="images/topp1.png" align="left" />
</a></td></tr>
<tr><td colspan="2" class="servHd" height="40"><?php echo utf8_encode($navn);?></td></tr>


<?php
$result3 = mysql_query("SELECT Lokasjonsnavn FROM {$table1} WHERE Lokasjons_id={$lokk} and Kunde_id='{$kundeid}'") or die(mysql_error());
while ($row3 = mysql_fetch_array($result3)){
    $navn = $row3['Lokasjonsnavn'];
?>
<tr>
	<td class="servBodL" ><?php echo utf8_encode($navn); ?></td>
</tr>
<?php
}

?>

</table>
<br>
<br><br>
<fieldset id="actions">
	    <br>
		<a href="http://www.softkey.no/webtest/gsistrerssqr.php?lokk=<?php echo $lokk; ?>&Kunde=<?php echo $kundeid; ?>&Navn=<?php echo  utf8_encode($navn); ?>&REGJN=1"><input type="submit" id="LOGIN" value="Scan"></a>
		
</fieldset>
</Form>

</body>
</html>

Open in new window


The problem is that the page return a blank page.

If i remove this code:
  $finneslokasjon = sjekk_om_flere_lokasjoner($lokk,$kundeid);

   if ($finneslokasjon==false)
   {
        header("location: http://www.softkey.no/webtest/maiside.php");
	    exit;
   }

Open in new window

its working fine ?

What is wrong with this code ?
0
Comment
Question by:team2005
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 39253129
On line 4 of your first code segment, try:
$res = mysql_query($sql) or die(mysql_error() . ':<br />' . $sql);

Open in new window

If the query is failing, it should reveal why it is failing.


Also, on line 5 of your second code segment, you probably meant to have "==" (two equal signs):
if ($uid == access_control(TRUE)){...}

Open in new window


Lastly, change "location" to "Location"  (Upper Case "L"):
header("Location:...");
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 39253161
0
 
LVL 56

Expert Comment

by:Julian Hansen
ID: 39253194
@hielo,

Also, on line 5 of your second code segment, you probably meant to have "==" (two equal signs):
if ($uid == access_control(TRUE)){...}

Be careful here - it is possible this was supposed to be an assignment and test in one go ...

You can also refactor your function no need for all the else statements leading to a return false - make your function return false by default and return true only when conditions for doing so are met.

function sjekk_om_flere_lokasjoner($lokasjon,$kundeid)
{
  $num = 0;
  $sql = "SELECT Lokasjons_id FROM Lokasjoner WHERE Lokasjons_id=$lokasjon and Kunde_id='$kundeid'";
    $res = mysql_query($sql);
        
    // IF THE QUERY SUCCEEDED
    if ($res) {
      // THERE SHOULD BE ONE ROW
      $num = mysql_num_rows($res);
    }
// For debugging purposes
   else {
      echo "Error: " . mysql_error();
   }
    return ($num > 0);
}

Open in new window

NB: the mysql lib is deprecated as of php 5.5 and is going to dissappear in future releases. Might be a good time to switch to PDO / mysqli.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:team2005
ID: 39253307
Hi!

Still the same problem -> blank page
and no php error message ?
0
 
LVL 56

Accepted Solution

by:
Julian Hansen earned 250 total points
ID: 39253521
Change your two header() calls to die() instead

i.e.
header('location: xxx') => die('location: xxx');

Run again - and post back what message is displayed.
0
 
LVL 22

Assisted Solution

by:Kim Walker
Kim Walker earned 250 total points
ID: 39253582
Do you have display errors turned off? This should turn them on if placed at the very beginning of your script:
ini_set('display_errors', '1');
error_reporting(E_ALL);

Open in new window

0
 
LVL 2

Author Comment

by:team2005
ID: 39253804
Hi!

Get this error messages:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/2/s/softkey/www/webtest/side3.php:1) in /home/2/s/softkey/www/webtest/RAY_EE_config.php on line 13

Warning: Cannot modify header information - headers already sent by (output started at /home/2/s/softkey/www/webtest/side3.php:1) in /home/2/s/softkey/www/webtest/side3.php on line 18
0
 
LVL 56

Expert Comment

by:Julian Hansen
ID: 39254109
That's something else.

Not enough info on how your code is structured to continue - what is side3.php ?
0
 
LVL 2

Author Comment

by:team2005
ID: 39254243
Hi!

Here is side3.php

<?php // RAY_EE_login.php
require_once('RAY_EE_config.php');
$table1 = 'Lokasjoner';
// ACCESS TO THIS PAGE IS TESTED BUT NOT CONTROLLED
if ($uid = access_control(TRUE))
{
   $brukerID = brukerident('xxx');
   $navn = $_GET['Navn'];
   $lokk = $_GET['lokk'];   
   $kundeid = kundeid();
   
   $finneslokasjon = sjekk_om_flere_lokasjoner($lokk,$kundeid);

   if ($finneslokasjon==false)
   {
        header("location: http://www.softkey.no/webtest/hovedside.php");
	    exit;
   }
}
else
{
        header("location: http://www.softkey.no/webtest/login.php");
	    exit;
}
?>
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8" />
<title>Test system</title>

</head>

<body>
<link rel="stylesheet" href="default.css">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<table summary="Services, or Links box template" class="servicesT" cellspacing="1">
<tr><td colspan="2" class="header1" height="20"><a href="http://www.softkey.no/webtest/hovedside.php" target="_self">
<img src="images/topp1.png" align="left" />
</a></td></tr>
<tr><td colspan="2" class="servHd" height="40"><?php echo $navn;?></td></tr>


<?php
$result3 = mysql_query("SELECT Lokasjonsnavn FROM {$table1} WHERE Lokasjons_id={$lokk} and Kunde_id='{$kundeid}'") or die(mysql_error());
while ($row3 = mysql_fetch_array($result3)){
    $navn = utf8_encode($row3['Lokasjonsnavn']);
?>
<tr>
	<td class="servBodL" ><?php echo $navn; ?></td>
</tr>
<?php
}

?>

</table>
<br>
<br><br>
<fieldset id="actions">
	    <br>
		<a href="http://www.softkey.no/webtest/registrerqr.php?lokk=<?php echo $lokk; ?>&Kunde=<?php echo $kundeid; ?>&Navn=<?php echo $navn; ?>&REGJN=1"><input type="submit" id="LOGIN" value="Scan"></a>
		
</fieldset>
</Form>

</body>
</html>

Open in new window

0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 39254283
Were the errors you posted above generated after you added the two lines for error display I recommended? I don't see them in side3.php and that would throw off the line numbering.

The issue is obviously that output is being generated before the session start command and before the header is issued. I must assume that a session is being started in the included "RAY_EE_config.php" file. The question is where and how is output being generated before that unless another error was generated before these two.

Be sure to post the ENTIRE list of errors and the latest version of side3.php.
0
 
LVL 56

Expert Comment

by:Julian Hansen
ID: 39255336
@team2005 - in order for us to help you we need information. The code you have posted does not appear to contain any of the changes recommended in this thread. We also appear to be dealing with moving targets - the original post was about a blank page now it is about warnings relating to headers being sent after output is sent to the browser.

We need to see what changed to create the second issue - which is not necessarily linked to the first.

Post the code that caused the error along with any supporting files (as attachments).

When attempting recommendations made in this thread feedback specifically to the person who gave you the recommendation so we know what post you are responding to otherwise it gets extremely confusing.
0
 
LVL 2

Author Closing Comment

by:team2005
ID: 39261953
Thanks
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
message Alert on an empty search 10 29
CSS Style 8 42
What cart is this? 2 33
Table header must be on top 2 25
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question