Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Undefined variable ? 1

Status
Not open for further replies.

pookie62

Technical User
Oct 22, 2004
139
GB
Hi,
Anyone sees why I'm getting undefine index for output ?
"Undefined index: id" in /path to/edit.php on line 57

Code:
// display initial form with values pre-filled
$submit='';
if (!$submit)
{
    // open database connection
    $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

    // select database
    mysql_select_db($db) or die ("Unable to select database!");

    // generate and execute query
    $slug='';
    $content='';
    $id='';

    $slug = addslashes($slug);
    $content = addslashes($content);
Line 57    $id= $_GET['id'];
    $query = "SELECT slug, content, contact FROM news WHERE id = '$id'";
    $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
    
    // if a result is returned
    if (mysql_num_rows($result) > 0)
    {
        // turn it into an object
        $row = mysql_fetch_object($result);

        // print form with values pre-filled
?>
<table cellspacing="5" cellpadding="5">
<form action="<? echo $PHP_SELF; ?>" method="POST">
<input type="hidden" name="id"  value="<? echo $id; ?>">
<tr>
 
Because the form is sending POST-method data and you're looking in $_GET.

If the form's method is "POST", you must look in $_POST.
If the form's method is "GET", you need to look in $_GET.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Hi sleipnir214,
Thanks for your quick reply.
Don't get the error nomore but updating the content is working now.
When I press the update button, the pages refreshes and content isn't changed..
any ideas ?
Let me post most of the script..
Code:
// generate and execute query
	$slug='';
	$content='';
	$id='';

	$slug = addslashes($slug);
	$content = addslashes($content);
	$id= $_GET['id'];
	$query = "SELECT slug, content, contact FROM news WHERE id = '$id'";
	$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
	
	// if a result is returned
	if (mysql_num_rows($result) > 0)
	{
		// turn it into an object
		$row = mysql_fetch_object($result);

		// print form with values pre-filled
?>
<table cellspacing="5" cellpadding="5">
<form action="<? echo $PHP_SELF; ?>" method="GET">
<input type="hidden" name="id"  value="<? echo $id; ?>">
<tr>
	<td valign="top"><b><font size="-1">Titel</font></b></td>
	<td><input size="50" maxlength="250" type="text" name="slug" value="<? echo $row->slug; ?>"></td>
</tr>
<tr>
	<td valign="top"><b><font size="-1">Inhoud</font></b></td>
	<td><textarea name="content" cols="40" rows="10"><? echo $row->content; ?></textarea></td>
</tr>
<tr>
	<td valign="top"><font size="-1">Contact persoon</font></td>
	<td><input size="50" maxlength="250" type="text" name="contact"  value="<? echo $row->contact; ?>"></td>
</tr>
<tr>
	<td colspan=2><input type="Submit" name="submit" value="Update"></td>
</tr>
</form>
</table>
<?
	}
	// no result returned
	// print graceful error message
	else
	{
		echo "<font size=-1>That press release could not be located in our database.</font>";
	}
}
// form submitted
// start processing it
else
{
	// set up error list array
	$errorList = array();
	$count = 0;
	
	// validate text input fields
	if (!$slug) { $errorList[$count] = "Invalid entry: Slug"; $count++; }
	
	if (!$content) { $errorList[$count] = "Invalid entry: Content"; $count++; }
	
	// set default value for contact person
	if (!$contact) { $contact = $def_contact; }
	
	// check for errors
	// if none found...
	if (sizeof($errorList) == 0)
	{
		// open database connection
		$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

		// select database
		mysql_select_db($db) or die ("Unable to select database!");

		// generate and execute query
		$query = "UPDATE news SET slug = '$slug', content = '$content', contact = '$contact', timestamp = NOW() WHERE id = '$id'";
		$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

		// print result
		echo "<font size=-1>Update successful. <a href=list.php>Go back to the main menu</a>.</font>";

		// close database connection
		mysql_close($connection);
	}
	else
	{
		// errors occurred
		// print as list
		echo "<font size=-1>The following errors were encountered: <br>";
		echo "<ul>";
		for ($x=0; $x<sizeof($errorList); $x++)
		{
			echo "<li>$errorList[$x]";
		}
		echo "</ul></font>";
	}
}
?>
 
First, use POST-method, not GET-method in your form. Only a small amount of information can be transmitted in a GET-method form submission, and GET clutters up the URL so.

As far as your script goes, I couldn't tell you. The script is incomplete -- there is more script code above there you began your post or should be. What does that "else" in line 49 match up with?



Want the best answers? Ask the best questions! TANSTAAFL!
 
Actually, it's part of a few scripts..
This is the script where content should become editable..
Please let me know if you need more info..
Appreciate the help !

Oke, this is the entire (edit.php)script:
Now with the POST in it, I get the undefined index again..
Code:
<?
// includes
include("../conf.php");
include("../functions.php");

// form not yet submitted
// display initial form with values pre-filled
$submit='';
if (!$submit)
{
	// open database connection
	$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

	// select database
	mysql_select_db($db) or die ("Unable to select database!");

	// generate and execute query
	$slug='';
	$content='';
	$id='';

	$slug = addslashes($slug);
	$content = addslashes($content);
	$id= $_POST['id'];
	$query = "SELECT slug, content, contact FROM news WHERE id = '$id'";
	$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
	
	// if a result is returned
	if (mysql_num_rows($result) > 0)
	{
		// turn it into an object
		$row = mysql_fetch_object($result);

		// print form with values pre-filled
?>
<table cellspacing="5" cellpadding="5">
<form action="<? echo $PHP_SELF; ?>" method="POST">
<input type="hidden" name="id"  value="<? echo $id; ?>">
<tr>
	<td valign="top"><b><font size="-1">Titel</font></b></td>
	<td><input size="50" maxlength="250" type="text" name="slug" value="<? echo $row->slug; ?>"></td>
</tr>
<tr>
	<td valign="top"><b><font size="-1">Inhoud</font></b></td>
	<td><textarea name="content" cols="40" rows="10"><? echo $row->content; ?></textarea></td>
</tr>
<tr>
	<td valign="top"><font size="-1">Contact persoon</font></td>
	<td><input size="50" maxlength="250" type="text" name="contact"  value="<? echo $row->contact; ?>"></td>
</tr>
<tr>
	<td colspan=2><input type="Submit" name="submit" value="Update"></td>
</tr>
</form>
</table>
<?
	}
	// no result returned
	// print graceful error message
	else
	{
		echo "<font size=-1>That press release could not be located in our database.</font>";
	}
}
// form submitted
// start processing it
else
{
	// set up error list array
	$errorList = array();
	$count = 0;
	
	// validate text input fields
	if (!$slug) { $errorList[$count] = "Invalid entry: Slug"; $count++; }
	
	if (!$content) { $errorList[$count] = "Invalid entry: Content"; $count++; }
	
	// set default value for contact person
	if (!$contact) { $contact = $def_contact; }
	
	// check for errors
	// if none found...
	if (sizeof($errorList) == 0)
	{
		// open database connection
		$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

		// select database
		mysql_select_db($db) or die ("Unable to select database!");

		// generate and execute query
		$query = "UPDATE news SET slug = '$slug', content = '$content', contact = '$contact', timestamp = NOW() WHERE id = '$id'";
		$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

		// print result
		echo "<font size=-1>Update successful. <a href=list.php>Go back to the main menu</a>.</font>";

		// close database connection
		mysql_close($connection);
	}
	else
	{
		// errors occurred
		// print as list
		echo "<font size=-1>The following errors were encountered: <br>";
		echo "<ul>";
		for ($x=0; $x<sizeof($errorList); $x++)
		{
			echo "<li>$errorList[$x]";
		}
		echo "</ul></font>";
	}
}
?>

<!-- standard page footer begins -->
<p>
<?php include ("../footer.php"); ?>
<!-- standard page footer ends -->
</body>
</html>
 
You're doing several odd things here.

First is these lines:

$submit='';
if (!$submit)

In this case, the condition "!$submit" will always evaluate to FALSE. Does this script actually output the form?



Want the best answers? Ask the best questions! TANSTAAFL!
 
You're doing several odd things here."

Hmm glad I just found this script.. ;-)
Oke, I commented out:
$submit='';

changed back
$id= $_GET['id'];

left this:
<form action="<? echo $PHP_SELF; ?>" method="POST">

Result:
Undefined index on line 42 =
if (!$submit)
{
// open database connection
But ! > Updating content works.. !
Weird he ? So.. back were we started.. LOL !
 
What is $submit? Unless your script instantiates it, your code is always going to balk at that line.

Maybe, just maybe, $_POST['submit'] will exist when the form this script produces submits back to this form. If your if-statement on line 42 is testing that input has been submitted to the form, perhaps performing the check

if (isset($_POST['submit']))

is more appropriate.



Want the best answers? Ask the best questions! TANSTAAFL!
 
I've tried several ways to fit in your suggestion but it's not getting any better..
D*mn.. was so close with this script..
I've no clue what to do now..
Should I post the other scripts ?
 
Frankly, I don't know.

I don't now what you have tried that didn't work. Nor am I completely sure what it was you were trying to do in the first place.

Let me ask very specific questions....[ol][li]What was your intent with the lines
[tt]$submit='';
if (!$submit)[/tt]
? What is the purpose of this entire if-else block? Is it to only run certain code when their is form input?[/li][li]What of my advice have you tried that did not work? All I can know is that I post something, you chew on it for a while and say, "but its not getting any better".[/li][/ol]




Want the best answers? Ask the best questions! TANSTAAFL!
 
Like I said, I found the script in a tutorial somewhere, and my knowledge of php is too little to sort things out myself..
Maybe this helps, the script from were all contentmessages are listed and through links can be deleted or editted.
Then we come in the edit script were I get this error.

Code:
<?
//list.php - display list of most recent press releases
?>
<html>
<head>
<basefont face="Verdana">
<SCRIPT language="JavaScript">
<!--
function confirm_delete(idval)
{
var verify= confirm("Do you really want to delete this?");
if (verify== true)
{
window.location="delete.php?id=" + idval;
}
}
//-->
</SCRIPT>
</head>

<body>

<!-- standard page header begins -->
<p>&nbsp;<p>

<table width="100%" cellspacing="0" cellpadding="5">
<tr>
	<td></td>
</tr>
<tr>
	<td bgcolor="Navy"><font size="-1" color="White"><b>Actionshooting Nederland : Beheer Ingezonden Nieuws</b></font></td>
</tr>
</table>
<!-- standard page header ends -->

<p>

<?
// includes
include("../conf.php");
include("../functions.php");

// open database connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// generate and execute query
$query = "SELECT id, slug, timestamp FROM news ORDER BY timestamp DESC";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

// if records present
if (mysql_num_rows($result) > 0)
{
	// iterate through resultset
	// print title with links to edit and delete scripts
	while($row = mysql_fetch_object($result))
	{
	?>
	<font size="-1"><b><? echo $row->slug; ?></b> [<? echo formatDate($row->timestamp); ?>]</font>
	<br>
	<font size="-2"><a href="edit.php?id=<? echo $row->id; ?>">edit</a> | <a href="javascript://" onclick="confirm_delete(<? echo $row->id; ?>); return false">delete</a></font>
	<p>
	<?
	}
}
// if no records present
// display message
else
{
?>
	<font size="-1">Geen nieuwsberichten beschikbaar</font><p>
<?
}

// close connection
mysql_close($connection);
?>
<font size="-2"><a href="add.php">nieuw item</a></font>

<!-- standard page footer begins -->
<p>
<?php include ("../footer.php"); ?>
<!-- standard page footer ends -->


</body>
</html>

Answering your questions:
1. the if(!$submit) statement was in the script but produces the error, therefor I added the $submit=''; to get rid of it. Without success, so I removed it again.

The if-else block (as far as I can see) looks if any changes are made in the content, then after the submit(button) produces a link back to the list script(mainmenu)

2. I replaced the if (!$submit) with your suggestion as you said it would be more appropriate.I didn't see the content to edit, so changed it back to what it was.

Sorry if I'm not being clear.. trying to..
 
Drat.

My apologies. My earlier post has a glaring typo it should have read:

if ([red]![/red]isset($_POST['submit']))

Sorry about that.



The problem with older tutorials is in how PHP's default behavor changed over time. In the Olden Days, PHP was configured by default so that when you have a form:

<form method="post" action="somescript.php">
<input type="text" name="foo"><input type="submit">
</form>

submit data, PHP would populate the value in the variables $HTTP_POST_VARS['foo'] and $foo. The array $HTTP_POST_VARS was replaced with $_POST, and eventually, PHP was configured by default to not create $foo. So a lot of tutorials out there expect $foo to be there then it isn't. Or in the case of your form and script, $submit is not there.

All of this is documented in the PHP online manual:


Want the best answers? Ask the best questions! TANSTAAFL!
 
That did it!!
Thank you so much for your time and patience..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top