Jump to content

Permenant variables


Suchy

Recommended Posts

On one page I have a loop that prints thumbnail images, and link to a page with the full image.

<a href="<?php print("image.php?url={$curResult['url']}"); ?>">
// where url is a url from mysql to the image, for ex. http://www.samplesite.com/images/75896.jpg

 

and then on the image page I have a variable that catches that url

$link = $_GET['url']; 

 

the full url of the image page looks like:

 

The problem is that when the page is refreashed the $link variable is empty so that the image does not show up, how can I fix this?

Link to comment
https://forums.phpfreaks.com/topic/50030-permenant-variables/
Share on other sites

Maybe there is something wrong with my code:

<?php ////////// image.php
session_start();
$link = $_REQUEST['url']; 
$_SESSION['ses'] = $link;
print_r($_SESSION);

.....

if(in("submit"))
{    
 $url = $_SESSION['ses']; 
 $id = $_POST['comment_id'];
 $name = mysql_real_escape_string($_POST['name']);
 $comment = mysql_real_escape_string($_POST['comment']);

$query = "UPDATE comments SET name = '$name' , comment = '$comment' WHERE photo_url = '$url' ";
 $result = mysql_query($query);
}

....

 

when the page is first loaded the print_r($_SESSION); prints out something like:

 

but after the submit button is pressed it is just:

 

Link to comment
https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-245875
Share on other sites

It is a very simple page:

<?php ////////// image.php
session_start();
$link = $_REQUEST['url']; 
$_SESSION['ses'] = $link;
print_r($_SESSION);

DEFINE (DB_USER, "*****");
DEFINE (DB_PASSWORD, "****");
DEFINE (DB_HOST, "*****");
DEFINE (DB_NAME, "*****");

connectDB();

$query = "SELECT comments.name, comments.comment FROM comments WHERE comments.photo_url = '$link' ";

$result = mysql_query($query);
$entriesResults = getRows($result);

function connectDB()
{
$db_connection = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db (DB_NAME);
}

function getRows($result) 
{
  $rows = array(); 
  if (mysql_num_rows($result) > 0) 
{
    while($row = mysql_fetch_array($result,MYSQL_ASSOC)) 
    {
      $rows[] = $row;
    }
  }
  return $rows;
}

function in($key)	
{
$value = $_POST[$key]; 
if (!isset($value)) 
            {	
	$value = $_GET[$key];
}		
return $value;
}

if(in("submit"))
{    
 $link = $_SESSION['ses']; 
 $id = $_POST['comment_id'];
 $name = mysql_real_escape_string($_POST['name']);
 $comment = mysql_real_escape_string($_POST['comment']);

              $query = "UPDATE comments SET name = '$name' , comment = '$comment' WHERE photo_url = '$link' ";
 $result = mysql_query($query);
}
?>

<html>
<head>
</head>
<body>
  
<?php
$x = 320;
$y = 240;
?>
  
  <img width="<? echo $x; ?>"  height="<? echo $y; ?>" src="<? echo $link; ?>"  border="0" > </img>
  <br>------------</p>
<p>

<?php foreach($entriesResults as $curResult) { ?>
name: <?php print($curResult['name']); ?>
<br>
comment: <?php print($curResult['comment']); ?>
<BR />**<br />
<? } ?>

</p>
<p>-----------</p>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>" enctype="multipart/form-data">
  <p>name<br />
      <input name="name" type="text" id="name" size="35" />
    <br />
    <br />
    comment  :<br />
    <textarea name="comment" cols="27" rows="5" id="comment" ></textarea>
  </p>
  <p>
    <input type="hidden" name="execute" value="1" />
  </p>
  <p>

<input name="submit" type="submit" id="submit" value="submit" />
</form>
</body>
</html>

 

This is the correct code, for the one above I copied from a wrong file, it should be:

$link = $_SESSION['ses'];

and not

$url = $_SESSION['ses'];

Link to comment
https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-245892
Share on other sites

Your problem is, at the top of your code, you define:

 

$link = $_REQUEST['url']

 

so $link is now http://path/to/img

 

Then you define:

 

$_SESSION['ses'] = $link

 

meaning $_SESSION['ses'] = $link = http://path/to/img

 

Then, when the form is submitted you define:

 

$link = $_SESSION['ses']

 

Which means that $link no longer has a value - because you just erased it.

 

Notice -

$_SESSION['ses'] = $link, and whatever $link is set to if you unset $link, by redefining it - then you also unset $_SESSION['ses']

 

So thats your problem.

 

Link to comment
https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-245914
Share on other sites

i had a quick look through and saw that you arew calling a function before it has been set..

 

put:

connectDB();

 

after:

function connectDB()
{
$db_connection = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db (DB_NAME);
}

 

same with your getRows function...

Link to comment
https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-246154
Share on other sites

Thats not it, since the problem is ste same plus I am getting mysql errors:

Warning: mysql_query() [function.mysql-query]: Access denied for user 'nobody'@'localhost' (using password: NO)

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

Link to comment
https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-246167
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.