Nikola Radovic Nikola Radovic - 1 year ago 38
PHP Question

Passed php variable from one file to another not in scope of if statement

How do I use a php variable which is transferred from one file (

search.inc.php
) into another (
anotherpage.php
) in if statement?

My connection with MySQL is correct but I can't use variable
$bilkon
(which is picked up from MySQL) in if statement in
anotherpage.php
.

When echo
$bilkon
in
anotherpage.php
, I see the "Yes" response, but when I use this
$bilkon
in if statement I don't get the correct response. I got the "Bad" response from the if statement although I got on echo answer "Yes".


search.inc.php


<?php
session_start();
require 'connect.php';

if(isset($_GET['search_text'])){
$search_text = $_GET['search_text'];

if(!empty($search_text)){

$query="SELECT * FROM kon WHERE sifkon LIKE '%$search_text%'";
$query_run = mysqli_query($con, $query);

while($query_row = mysqli_fetch_assoc($query_run)){

echo $sifkon='<a href="anotherpage.php?search_text='.$query_row['sifkon'].'">'.$query_row['sifkon'].' '.'</a>';
echo $nazkon = $query_row['nazkon'].'<br>';
$bilkon = $query_row['bilkon'].'<br>';
$strknj = $query_row['strknj'].'<br>';
$devknj = $query_row['devknj'].'<br>';
$orjkon = $query_row['orjkon'].'<br>';
$tiporj = $query_row['tiporj'].'<br>';
$_SESSION['test']=$bilkon;
}
}
}
?>



anotherpage.php


<?php
include 'search.inc.php';

echo $_SESSION['test'];//Yes
if ($_SESSION['test'] == 'Yes'){
echo 'Good';
}else {
echo 'Bad';
}
?>

Answer Source

$bilkon has a <br> at the end, you add it, but you dont check for that

if ($_SESSION['test'] == 'Yes<br>'){

better option to remove the <br> until you need it.

for future debugging use

var_dump($_SESSION['test']);

instead of echo, it catches things like html, white space, etc.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download