Page 1 of 1

[56K WARN] lost $id variable in $_POST

Posted: Tue Jul 31, 2007 5:17 pm
by gregorious
I am trying to get some radio buttons to work, but before I even got into the logic of radio buttons I ran into a snag with my $id variable not being avaiable for the whole page [speifically - the top $_POST portion of the page].

Below is a condensed version of my page code. There is are two variables passed to this page from the previous page: $id and $imageid

Code: Select all

<html>

<head>
	<title>Untitled 3</title>
</head>
<body>

<?php 
if (isset($_POST['editimage'])):
	$imageresorder = $_POST['imageresorder'];
	$imagerescaption = $_POST['imagerescaption'];
	$imageid = $_POST['imageid'];
		  
		  
	$sqlupdate = "UPDATE image_res SET      
	image_res_order='$imageresorder',
	image_res_caption='$imagerescaption' 
	WHERE image_id='$imageid'";
							  
if (@mysql_query($sqlupdate)) {							  
							  
          echo "Residential IMAGE updated!";
          
  } else {

          echo "Error UPDATING database " . mysql_error() . "Call webmaster";
		  }
		  

// $setfeaturepic = "UPDATE residential SET 
//  res_feature_pic='$imageid',
//  res_id='$id'		          
//  WHERE res_id='$id'";

// if (!mysql_query($setfeaturepic)) {
//   echo ("<p>Error UPDATING Feature Picture!"
//        . $imageid . "Call webmaster");
//			  } 
			  
?>


<?php

else: 
		
     $id = $_GET['id'];
       $imageget = @mysql_query("SELECT 
       	image_id,
       	image_res_id,				         
       	 image_res_order,
       	image_res_caption
	FROM image_res, residential 
	WHERE image_res_id='$id' AND image_id='$imageid'");
				  
        if (!$imageget) {
        echo("Error SELECTING data from database!" . mysql_error() . "Call webmaster");
        exit();
        }
        while ($imgarray = mysql_fetch_array($imageget)) {
        	$imageid = htmlspecialchars($imgarray["image_id"]);        	
        	$imageresorder = htmlspecialchars($imgarray["image_res_order"]);
        	$imagerescaption = htmlspecialchars($imgarray["image_res_caption"]);
			}         	
?> 

        	
<?php endif; ?>  	
  	
<form action='<?php echo $_SERVER[PHP_SELF];?>' method='post'>

<table width='300' border='0' cellspacing='0' cellpadding='0'>
<tr>
<td width='150' align='left' valign='top'>
<span class='caption_cap'>Display Order</span><br>            
<input name='imageresorder' class='body01' size='3' maxlength='3' value='<?php echo $imageresorder;?>'><br>
</td>
		    
<td width='150' align='left' valign='top'><span class='caption_cap'>Feature Picture</span><br>
<input name='featurepic' type='radio' value='<?php $image_res_id;?>' ><span class='caption_cap'>yes</span>
<input name='featurepic' type='radio' value='' checked='' ><span class='caption_cap'>no</span>
</td>            
</tr>
</table> 
		   
<input name='imageid' type='hidden' value='<?php echo $imageid;?>' >  
		             
<span class='caption_cap'>Picture Caption</span><br>
<input name='imagerescaption' class='body01' size='36' maxlength='64' value='<?php echo $imagerescaption;?>'>
		   
<br>
<div align='center'><input type='submit' name='editimage' class='body01' value=' SAVE '></div>
</form>  	


<?php
$resselect = @mysql_query("SELECT 
	res_street01,
	res_city,
	FROM residential WHERE res_id='$id'");
		              
            if (!$resselect) {
            echo("Error selecting RESIDENTIAL data from database!" . mysql_error() . "Call webmaster");
            exit();
            }

            while ($resarray = mysql_fetch_array($resselect)) {	
            	
            	$resstreet01 = htmlspecialchars($resarray["res_street01"]);
            	$rescity = htmlspecialchars($resarray["res_city"]);          	
	}
				
?>  
	
<div align="center"><span class="caption_cap">Record Id&nbsp;&nbsp; <?php echo $id; ?></span></div>
<hr align='center' width='450' size='1' noshade>
	   
<table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
<tr><td align="left" width="200"valign="top">
<span class="listing01">
<strong><?php echo $restype ;?></strong><br>
<?php echo $resstreet01 ;?><br>
<?php echo $rescity ;?></span> 			
</td>
</tr>
</table><br> 	
	
<?php

       $imgselect = @mysql_query("SELECT
	image_id,
       	image_res_order,
       	image_res_caption,       		   
	FROM image_res 
	WHERE image_res_id='$id' ORDER by image_res_order");
        if (!$imgselect) {
        echo    ("Error SELECTING data from database!" . mysql_error() . "Call webmaster");			   
        exit();
        }
         while ($imgarray = mysql_fetch_array($imgselect)) {
        	$imageresorder = htmlspecialchars($imgarray["image_res_order"]);
        	$imagerescaption = htmlspecialchars($imgarray["image_res_caption"]); 	
  	
  	
 echo      "<table width='340'  align='center' border='0' cellspacing='0' cellpadding='0'>
            <tr>
	<td width='170'><span class='body01'><strong>$imageresorder </strong></span>
	<span class='caption_cap'>Display Order</span></td>
			
	<td width='170' align='left'><br><span class='caption_cap'>Picture Caption</span><br>
            <span class='body01'><strong>$imagerescaption</strong></span></td>
	</tr>
	</table>";
	}

?>
  	
  	
       <?php @require_once('../includes/copyright.php'); ?> 	
  	
<?php mysql_close() ?>
</body>
</html>
The commented portion is my radio button code, that is where I need the $id variable - in the WHERE mysql statement.

The $id tag seems to work everywhere else. What am I missing? Is there an "eregi (left-brain && hip-check)" function I need to study?

.

Posted: Tue Jul 31, 2007 5:40 pm
by nathanr
your not putting the id into $id until half way down the page mate.. at this line

Code: Select all

$id = $_GET['id'];
put it at the top instead (before <html>), problem solved!

still lost $id value in $_POST

Posted: Tue Jul 31, 2007 7:51 pm
by gregorious
I just tried moving the $id = $_GET['id']; around and test my moving echo $id; around - still have the same issue.

The $id dead zone is in the code below; every where else, the test returns the $id value.

Code: Select all

if (isset($_POST['editimage'])):
	$imageresorder = $_POST['imageresorder'];
	$imagerescaption = $_POST['imagerescaption'];
	$imageid = $_POST['imageid'];
		  
		  
	$sqlupdate = "UPDATE image_res SET      
		image_res_order='$imageresorder',
		image_res_caption='$imagerescaption' 
		WHERE image_id='$imageid'";
							  
if (@mysql_query($sqlupdate)) {							  
							  
          echo "Residential IMAGE updated!";
          
  } else {

          echo "Error UPDATING database " . mysql_error() . "Call webmaster";
	}
		  

$setfeaturepic = "UPDATE residential SET 
		res_feature_pic='$imageid',
		res_id='$id'		          
		WHERE res_id='$id'";

	if (!mysql_query($setfeaturepic)) {
              echo ("<p>Error UPDATING Feature Picture!" . $imageid . "Call webmaster");
	} 
			  
?>


<?php

            else:

lost $id in $_POST statement

Posted: Tue Jul 31, 2007 9:05 pm
by gregorious
I tried the placing the $id = (int)$_GET['id']; tag as recomended, but the $id variable is dead after the if (isset($_POST['editimage'])): statement and begins again after else: staement

Code: Select all

<body> 

<?php 

   $id = (int)$_GET['id'];
echo $id;        <-----------works!
   if (isset($_POST['editimage'])):
          $imageresorder = $_POST['imageresorder'];
          $imagerescaption = $_POST['imagerescaption'];
          $imageid = $_POST['imageid'];

   $sqlupdate = "UPDATE image_res SET
          image_res_order='$imageresorder',
          image_res_caption='$imagerescaption'
          WHERE image_id='$imageid'";

         if (@mysql_query($sqlupdate)) { 

        echo "Residential IMAGE updated! $id "; <-----does not work!
This is the last hitch and I can put this project away... don't you know that is the way of things.

Posted: Tue Jul 31, 2007 9:45 pm
by Zoxive

Code: Select all

print '<pre>'; print_r ($_GET); print '</pre>'; // Debug $_GET Array Put this at the top of your code
Does your <form> post with $_GET variables?

$_SERVER['PHP_SELF'] does not include get variables...

form uses a POST to send the data back to the database

Posted: Tue Jul 31, 2007 10:14 pm
by gregorious
The form uses a POST to send the data back to the database;

Code: Select all

<form action='<?php echo $_SERVER[PHP_SELF];?>' method='post'>

and the form uses a GET to retreive the store data from the database.

Code: Select all

else: 
                
     $id = $_GET['id']; 
       $imageget = @mysql_query("SELECT 
        image_id, 
        image_res_id,                         
         image_res_order, 
        image_res_caption 
        FROM image_res, residential 
        WHERE image_res_id='$id' AND image_id='$imageid'");
the page is an EDIT page, and looks something like this; the EDIT and DELTE links are gone:

http://www.forthosewhowait.com/images/cms-editpage.jpg

I will try the debug array code now thanks.

I may not be the sharpest tool in the shed

Posted: Tue Jul 31, 2007 10:44 pm
by gregorious
I may not be the sharpest tool in the shed; but my testing
makes mesuspect these two colons. Outside of the colons
the $id variable works.


<?php
if (isset($_POST['editimage'])):

<?php
else:

Code from orignial message in this tread. Anyone got a clue?

added new JPG to help clarify the page design.

Posted: Wed Aug 01, 2007 11:06 am
by gregorious
I have added a jpg to this thread to help clarify the page design.

http://www.forthosewhowait.com/images/c ... page02.jpg

The POST / ELSE removes the form and reports a successful upload when the user submits.

I have tried POST and GET to bring the $id variable into the POST / ELSE but nothing seems to bring the value into that dead zone.

All help is appreciated

.

URL being passed from the previous page

Posted: Wed Aug 01, 2007 11:58 am
by gregorious
This is the URL being passed from the previous page.

Code: Select all

http://localhost/admin/edit_res_image.php?id=136&imageid=236
I find it intersesting that the $imageid is being passed through out the page, but not the $id

Posted: Wed Aug 01, 2007 12:09 pm
by iknownothing
There is nothing wrong with that URL

I just looked at what you posted above:

Code: Select all

echo "Residential IMAGE updated! $id "; <-----does not work!
Is that all your problem is?? the variable is not part of the string, so obviously it wont echo correctly...

Code: Select all

echo "Residential IMAGE updated! " . $id;

testing for the $id vairable in that message

Posted: Wed Aug 01, 2007 12:22 pm
by gregorious
iknownothing wrote:There is nothing wrong with that URL

I just looked at what you posted above:

Code: Select all

echo "Residential IMAGE updated! $id "; <-----does not work!
Is that all your problem is?? the variable is not part of the string, so obviously it wont echo correctly...

Code: Select all

echo "Residential IMAGE updated! " . $id;
I was testing for the $id vairable in that message; there is one little area in the code [first message in thread] that does not return the $id variable, although the $imageid variable does show up.

Greg

SOLUTION! -ugh! too easy

Posted: Wed Aug 01, 2007 12:58 pm
by gregorious
This is the second time a hidden value in the form has cost me hours on this project. I am going to learn from this one.

SOLUTION! - to pass FORM variables back up to the POST they must be declared in the FORM.

Code: Select all

<input name='id' type='hidden' value='<?php echo $id;?>' >
Only one question left... is there a willing hair donor out there?