$_GET not working

On this page I have:

<?php  echo "<p><a href='tracking-edit.php?cvid=$cvid'>Made a mistake above? Click to edit.</a></p>"; ?>

On the referenced page I have:

$cvid = $_GET['cvid'];	 
$cvid = mysql_real_escape_string($cvid);

$cover_tiers = @mysql_query("SELECT mag_name, mag_month, mag_year 
FROM cover_tiers 
WHERE id='$cvid'
");
if (!$cover_tiers) {
	exit('<p>Error fetching details. ' . mysql_error() . '</p>'); }

I keep getting the error:

Notice: Undefined index: cvid in C:\wamp\www\MEDIA_TRACKING\ racking-edit-COVER_TIERS.php on line 87

This is line 87: $cvid = $_GET[‘cvid’];

Why is the error being thrown?

Thanks,
Steve

Is the value actually being set in the html link? - I know you probably think it is but it looks to me like it might not be being set for some reason so when the link is clicked php doesn’t see it.

Can you show us the html for the link?

Also can you var_dump $_GET please?

I used to get the same error. You need to do this:


<?php
if(isset($_GET['cvid'])) {
$cvid = $_GET['cvid'];
}
?>

This stopped the error.

Earlier on the page I have:

$cover_tiers = mysql_query("INSERT INTO cover_tiers SET
mag_name = '$mag_name',
mag_month = '$mag_month',
mag_year = '$mag_year',
");
 
 $cvid = mysql_insert_id();

That’s where I get the $cvid from. The insert works fine.

But that only hides the error. It doesn’t solve why its happening in the first place. The Op wants to know why its not set when its being output in the hyperlink. Thats the problem hiding the error doesn’t help as the error as actually useful in telling us there is something wrong.

It’s because $_GET[‘cvid’] isn’t set. The error can be elimianted by the use of isset()


if (isset($_GET['cvid'])) {
$cvid = $_GET['cvid']
} else {
$cvid = 0; // Sets a default value
}


You need to look at the code that generates the value of $cvid in this line

 <?php  echo "<p><a href='tracking-edit.php?cvid=$cvid'>Made a mistake above? Click to edit.</a></p>"; ?>  

So is the value definitely in the html for the hyperlink when you look at it in the browser? - Can you show us the html the browser is seeing?

It’s not stopping the error this time, sorry!

Thanks,
Steve

Yes, the browser shows the correct id:

http://localhost/tracking-edit.php?cvid=23

Thats odd then because it looks like some sort of php problem…

Can you post the source for the whole script? - Maybe there is something else at play here…

Also can you var_dump $_REQUEST ?

Here’s the page:

<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="tracking.css" />

<title>Ad and Editorial Tracking: COVER TIERS EDIT ENTRY</title>


</head>

<body>

<?php
require_once ('tracking-CONNECT.php'); 
include ('tracking-retrieve-inc_RETURN.php');


/* ////////////////////////////////////////////////////////
Insert COVER_TIERS table editing
//////////////////////////////////////////////////////// */

/* Checking to see if form was filled out for editing the table. If so, update the table. If not, show the fields for editing. */
if (isset($_POST['mag_issue']))
{
$cvid = $_POST['cvid'];
$mag_name = $_POST['mag_name'];
$mag_month = $_POST['mag_month'];
$mag_year = $_POST['mag_year'];

$sqla = "UPDATE cover_tiers SET
mag_name = '$mag_name',
mag_month = '$mag_month',
mag_year = '$mag_year'
WHERE id='$cvid'";

if (@mysql_query($sqla)) { $inserta = 1;
	 } else {
	 exit('<p>Updating cover tiers table failed. Database not updated.' . mysql_error() . '</p>'); 
	 }


echo "<table class='form-table'><tbody>	<tr>";
echo "<td class='row-header' colspan='3'>";
echo "<h3 style='text-align:center'>DATA ENTRY RESULTS</h3></td></tr>";
echo "<tr><td class='row1'><p>You inserted:</p>";
echo "<p>Magazine name: " . $mag_name;
echo "<br />Magazine month: " . $mag_month;
echo "<br />Magazine year: " . $mag_year . "</p><br />";
		
echo "<br /><br />Cover Tiers table ID: " . $cvid. "</p>";
echo "</td></tr><tr><td class='row2'>";
echo "<p><a href:'tracking-edit.php?cvid=$cvid'>Click to edit</a></p>";
echo "</td></tr></table><br />";
echo "<div class='go'><p><a href='tracking-entry.php'>Return to Tracking Entry</a></p></div>";
exit();

} // if (isset) closing tag

/* ////////////////////////////////////////////////////////
Edit COVER_TIERS table
how-to: BYODB book, p.113-114
//////////////////////////////////////////////////////// */

/* Populate the fields for editing. */
else {


$cvid = $_GET['cvid']) {
$cvid = mysql_real_escape_string($cvid); }

$cover_tiers = @mysql_query("SELECT mag_name, mag_month, mag_year 
FROM cover_tiers 
WHERE id='$cvid'
");
if (!$cover_tiers) {
	exit('<p>Error fetching cover tier details. ' . mysql_error() . '</p>'); }
	
$cover_tiers = mysql_fetch_array($cover_tiers);
$mag_name = mysql_real_escape_string($cover_tiers['mag_name']);
$mag_month = mysql_real_escape_string($cover_tiers['mag_month']);
$mag_year = mysql_real_escape_string($cover_tiers['mag_year']);
 ?>
 
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p><strong>Edit the entry:</strong><br />
<label>Magazine name: <input type="text" name="Magazine name" value="<?php echo $mag_name; ?>" /></label><br />
<label>Magazine month: <input type="text" name="Magazine month" value="<?php echo $mag_month; ?>" /></label><br />
<label>Magazine year: <input type="text" name="Magazine year" value="<?php echo $mag_year; ?>" /></label><br />
<input type="hidden" name="cvid" value="<?php echo $cvid; ?>" />
<input type="submit" value="Submit Changes" /></p>
</form>
<div class='go'><p><a href='tracking-entry.php'>Return to Tracking Entry</a></p></div>

<?php }  ?>

</body></html>

string ‘26’ (length=2)

I think right here:

[COLOR=#000000]$cvid = $_GET[‘cvid’] {
$cvid = mysql_real_escape_stringCOLOR=#007700; }

//change to:

if(isset($_GET[‘cvid’])) {
[/COLOR][/COLOR]$cvid = $_GET[‘cvid’];
}else{
$cvid = “default value”;
}

$cvid = mysql_real_escape_stringCOLOR=#007700; } [/COLOR]

Also, I don’t think the curly bracket after:

$cvid = mysql_real_escape_string($cvid); }


is needed.

But that still doesn’t explain why the value is not being passed. The dump clearly shows its not there.

StevenHu if you have Teamviewer installed I can take a look with you thsi evening (UK time).

Try changing the


$cvid = $_GET['cvid']) { 

to


$cvid = $_POST['cvid']) { 

as your using POST for the form method

Since when did a hyperlink become $_POST ? - look at the very top of the page he is using a hyperlink. Yes the source for the page does use the same value as a hidden element in a form but if you look at the script again you can see that is processed elsewhere using $_POST.

The ops problem is that the hyperlink isn’t passing the variable in $_GET.

$cvid = $_GET['cvid']) { 

to

$cvid = $_GET['cvid'] { 

You’ve got a stray ) and a stray { in there

Seems like you have full error reporting enabled. The error is actually a notice which you can turn off by changing the “error_reporting” value inside php.ini or inside the code.

Anyway, the problem is there and you should check for the presence of the key inside an array before attempting to access it. Lots of ways to do this. Here is the simplest one of them all:

$cvid = @$_GET['cvid'];
        ^---- error suppression operator

Alternate way is to use is_set():

$cvid = isset($_GET['cvid']) ? $_GET['cvid'] : '';

Yet another way is to use array_key_exists():

$cvid = array_key_exists('cvid', $_GET) ? $_GET['cvid'] : '';