Suchy Posted May 4, 2007 Share Posted May 4, 2007 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: http://www.samplesite.com/image.php?url=http://www.samplesite.com/images/75896.jpg 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? Quote Link to comment https://forums.phpfreaks.com/topic/50030-permenant-variables/ Share on other sites More sharing options...
zzman Posted May 4, 2007 Share Posted May 4, 2007 Store it in a session var. Quote Link to comment https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-245577 Share on other sites More sharing options...
Suchy Posted May 4, 2007 Author Share Posted May 4, 2007 How can I do that? Also would this cause a problem, when the user selects a different photo, since the variable still will be $link = $_GET['url']; but the url will be different (ex. http://www.samplesite.com/images/92945.gif) Quote Link to comment https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-245584 Share on other sites More sharing options...
zzman Posted May 4, 2007 Share Posted May 4, 2007 read up ... http://us.php.net/session Quote Link to comment https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-245595 Share on other sites More sharing options...
Suchy Posted May 4, 2007 Author Share Posted May 4, 2007 Sessions work when I refresh the page manualy, but I have a form on the page and when some one hits the submit button the page reloads and then the session does not work. Any other ways to fix the problem? Quote Link to comment https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-245702 Share on other sites More sharing options...
yzerman Posted May 4, 2007 Share Posted May 4, 2007 try changing $_GET['url'] to $_REQUEST['url'] Quote Link to comment https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-245725 Share on other sites More sharing options...
Suchy Posted May 5, 2007 Author Share Posted May 5, 2007 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: Array ( => http://www...../97230.jpg [ses] => http://www...../97230.jpg ) but after the submit button is pressed it is just: Array ( => [ses] => ) Quote Link to comment https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-245875 Share on other sites More sharing options...
benjaminbeazy Posted May 5, 2007 Share Posted May 5, 2007 what is your form action? your variables get redefined when you post and if your form action is just "image.php" it will lose the vars Quote Link to comment https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-245880 Share on other sites More sharing options...
benjaminbeazy Posted May 5, 2007 Share Posted May 5, 2007 please post full code... Quote Link to comment https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-245882 Share on other sites More sharing options...
Suchy Posted May 5, 2007 Author Share Posted May 5, 2007 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']; Quote Link to comment https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-245892 Share on other sites More sharing options...
yzerman Posted May 5, 2007 Share Posted May 5, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-245914 Share on other sites More sharing options...
Suchy Posted May 5, 2007 Author Share Posted May 5, 2007 So what is the proper way to fix this? Quote Link to comment https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-246148 Share on other sites More sharing options...
Destramic Posted May 5, 2007 Share Posted May 5, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-246154 Share on other sites More sharing options...
Suchy Posted May 5, 2007 Author Share Posted May 5, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-246167 Share on other sites More sharing options...
MadTechie Posted May 5, 2007 Share Posted May 5, 2007 on the page you go to, add a hidden field with the same name and set the default text to the id, Quote Link to comment https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-246174 Share on other sites More sharing options...
yzerman Posted May 5, 2007 Share Posted May 5, 2007 So what is the proper way to fix this? remove $link = $_SESSION['ses'] from your if (in("submit")) statement? Quote Link to comment https://forums.phpfreaks.com/topic/50030-permenant-variables/#findComment-246241 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.