Click to See Complete Forum and Search --> : lost $id variable in $_POST


gregorious
07-31-2007, 09:05 PM
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

<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?

tried the placing the $id = (int)$_GET['id']; tag see below, but the $id variable is dead after the if (isset($_POST['editimage'])): statement and begins again after else: staement

<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.

JayM
07-31-2007, 09:11 PM
If your form action is post, you need to use $_POST['id'].
If your form action is get, you need to use $_GET['id'].

This is a fundamental concept that you must understand. You can not mix and match throughout a page. It's either one or the other.

Kostas Zotos
07-31-2007, 09:25 PM
Hi,

Or use the $_REQUEST['id'].


I don't get in depth in your code snippets, but i think that the $id variable normally should be available throughtout your code and maybe the problem is elsewhere..

Instead of:
echo "Residential IMAGE updated! $id "; <-----does not work!

Try something from these:
echo 'Residential IMAGE updated! '.$id.' ';
or
echo "Residential IMAGE updated! {$id} ";

Just a suggestion..

gregorious
07-31-2007, 09:53 PM
If your form action is post, you need to use $_POST['id'].
If your form action is get, you need to use $_GET['id'].

This is a fundamental concept that you must understand. You can not mix and match throughout a page. It's either one or the other.

I am new to PHP [about a year now] and the book I used was written a while back; still learning to code without register globals on.

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

I used GET to retrieve the stored values from the database; and POST to write the new data into the datsbase.

Seems to work okay, but I am all for learning "good form".

gregorious
07-31-2007, 10:49 PM
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?

gregorious
08-01-2007, 10:55 AM
I have added a jpg to this thread to help clarify the page design.

http://www.forthosewhowait.com/images/cms-editpage02.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.

gregorious
08-01-2007, 11:52 AM
This is the URL being passed from the previous page.

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

gregorious
08-01-2007, 12:54 PM
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.
<input name='id' type='hidden' value='<?php echo $id;?>' >

Only one question left... is there a willing hair donor out there?