Undefined variable/index

Query a MySQL database

Page 1 of 1

9 Replies - 1707 Views - Last Post: 14 September 2009 - 10:19 AM Rate Topic: -----

#1 1cookie  Icon User is offline

  • D.I.C Regular

Reputation: -5
  • View blog
  • Posts: 338
  • Joined: 19-October 06

Undefined variable/index

Posted 13 September 2009 - 06:09 AM

hi

I'm trying to construct a database driven website. The connection to the DB is fine; but my script produces a couple of errors. :) Ive attached a jpg showing the result of the errors. My code is:


<HTML>
<HEAD>
<TITLE> Our List of Jokes </TITLE>
</HEAD>
<BODY>

<?php
  // If the user wants to add a joke
  if ( isset($_POST['addjoke']) ):
?>

<FORM ACTION="<?php echo( $_POST['PHP_SELF'] ); ?>" METHOD=POST>
<P>Type your joke here:<BR>
<TEXTAREA NAME="joketext" ROWS=10 COLS=40 WRAP>
</TEXTAREA><BR>
<INPUT TYPE=SUBMIT NAME="submitjoke" VALUE="SUBMIT">
</FORM>

<?php else:

  // Connect to the database server
  $dbcnx = @mysql_connect("localhost", "root", "mypassword");
  if (!$dbcnx) {
	echo( "<P>Unable to connect to the " .
		  "database server at this time.</P>" );
	exit();
  }

  // Select the jokes database
  if (! @mysql_select_db("Jokes",$dbcnx) ) {
	echo( "<P>Unable to locate the joke " .
		  "database at this time.</P>" );
	exit();
  }

  // If a joke has been submitted,
	// add it to the database.
	if ("SUBMIT" == $_SERVER['submitjoke'] ) {
	  $sql = "INSERT INTO Jokes SET " .
			 "JokeText='$joketext', " .
			 "JokeDate=CURDATE()";
	  if (mysql_query($sql)) {
		echo("<P>Your joke has been added.</P>");
	  } else {
		echo("<P>Error adding submitted joke: " .
			 mysql_error() . "</P>");
	  }
	}
  
  
echo ("<P> Here are all the jokes in our database: </P>
<BLOCKQUOTE>");

  
  
  
  // Request the text of all the jokes
  $result = mysql_query(
			"SELECT JokeText FROM Jokes");
  if (!$result) {
	echo("<P>Error performing query: " .
		 mysql_error() . "</P>");
	exit();
  }

  // Display the text of each joke in a paragraph
  while ( $row = mysql_fetch_array($result) ) {
	echo("<P>" . $row["JokeText"] . "</P>");
  }

 echo("</BLOCKQUOTE>");
 
// When clicked, this link will load this page
	// with the joke submission form displayed.
	echo("<P><A HREF='$_POST[PHP_SELF]?addjoke=1'>" .
		 "Add a Joke!</A></P>");
 
 ?>
 
 <?php endif; ?>


</BODY>
</HTML>



The lines of code that a causing the errors are:

	if ("SUBMIT" == $_SERVER['submitjoke'] ) {




and

	echo("<P><A HREF='$_POST[PHP_SELF]?addjoke=1'>" .
		 "Add a Joke!</A></P>");




Ive tried changing

	if ("SUBMIT" == $_SERVER['submitjoke'] ) {


to:

	if ("SUBMIT" == $submitjoke) {




but both these choices give me errors: "Undefined index: submitjoke" and "Undefined variable: submitjoke" respectively.


It's the same scenario when i change:


	echo("<P><A HREF='$_POST[PHP_SELF]?addjoke=1'>" .
		 "Add a Joke!</A></P>");




to

	echo("<P><A HREF='$PHP_SELF?addjoke=1'>" .
		 "Add a Joke!</A></P>");





Finally, the Add a Joke! link at the bottom of the page does nothing! It is supposed to launch a form which enables a visitor to submit a joke to the MySQL database. However, all it actually does is change the url to: http://localhost/tes...t.php?addjoke=1 :)



Help please...

Attached File(s)


This post has been edited by 1cookie: 13 September 2009 - 06:11 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Undefined variable/index

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Undefined variable/index

Posted 13 September 2009 - 07:50 AM

I think in the example that uses:$_POST[PHP_SELF] you actually want to be using:$_SERVER[PHP_SELF], this is what equates to using $PHP_SELF, unless you have an expected post variable on this page named PHP_SELF?

PHP_SELF returns the current path, I assume the file that this is all located in is called JokeText.php? Since that is the path being returned. It is acting exactly how it should. An anchor (a tag) loads a link's href as a page (without a target) in the same tab/window that it was clicked in. That is exactly what it is doing, the URL just happens to have a get variable on it now, the page IS reloading as it should.

What exactly are you trying to accomplish with this link?
Are you trying to do a dynamic page change, eg: DOM manipulation to display a form? Or are you trying to load a new page that has the form on it, either within this page or as a new page?
Was This Post Helpful? 1
  • +
  • -

#3 1cookie  Icon User is offline

  • D.I.C Regular

Reputation: -5
  • View blog
  • Posts: 338
  • Joined: 19-October 06

Re: Undefined variable/index

Posted 14 September 2009 - 03:07 AM

View PostWilliam_Wilson, on 13 Sep, 2009 - 06:50 AM, said:

What exactly are you trying to accomplish with this link?
Are you trying to do a dynamic page change, eg: DOM manipulation to display a form? Or are you trying to load a new page that has the form on it, either within this page or as a new page?


hi WW

I'm trying to allow a user to type a joke and add it to my database. I want to slot it into my existing joke viewing page in a useful fashion. Since most users will only want to view my jokes, I don't want to mar the page with a big, ugly form unless the user expresses an interest in adding a new joke. For this reason, the application is well suited for implementation as a multi-purpose page. :)

I now feel a little silly as i'm thinking i should have included the source of the code for this idea. :blink:
Was This Post Helpful? 0
  • +
  • -

#4 1cookie  Icon User is offline

  • D.I.C Regular

Reputation: -5
  • View blog
  • Posts: 338
  • Joined: 19-October 06

Re: Undefined variable/index

Posted 14 September 2009 - 05:01 AM

View PostWilliam_Wilson, on 13 Sep, 2009 - 06:50 AM, said:

Or are you trying to load a new page that has the form on it, either within this page or as a new page?


Yes, something along these lines. :) As long as the form updates my database then that would be brill! :)

This post has been edited by 1cookie: 14 September 2009 - 05:02 AM

Was This Post Helpful? 0
  • +
  • -

#5 1cookie  Icon User is offline

  • D.I.C Regular

Reputation: -5
  • View blog
  • Posts: 338
  • Joined: 19-October 06

Re: Undefined variable/index

Posted 14 September 2009 - 06:41 AM

Ive made some changes, and i'm getting some functionality (but not the desired result).

My code now looks like:


<HTML>
<HEAD>
<TITLE> Our List of Jokes </TITLE>
</HEAD>
<BODY>

<?php
  // If the user wants to add a joke
  if ( isset($_GET['addjoke'] ) ):
?>

<FORM ACTION="<?php echo( $_SERVER['PHP_SELF'] ); ?>" METHOD=POST>
<P>Type your joke here:<BR>
<TEXTAREA NAME="joketext" ROWS=10 COLS=40 WRAP>
</TEXTAREA><BR>
<INPUT TYPE=SUBMIT NAME="submitjoke" VALUE="SUBMIT">
</FORM>

<?php else:

  // Connect to the database server
  $dbcnx = @mysql_connect("localhost", "root", "******");
  if (!$dbcnx) {
	echo( "<P>Unable to connect to the " .
		  "database server at this time.</P>" );
	exit();
  }

  // Select the jokes database
  if (! @mysql_select_db("Jokes",$dbcnx) ) {
	echo( "<P>Unable to locate the joke " .
		  "database at this time.</P>" );
	exit();
  }

  // If a joke has been submitted,
	// add it to the database.
	if ("SUBMIT" == $_POST['submitjoke']) {
	  $sql = "INSERT INTO Jokes SET " .
			 "JokeText='$joketext', " .
			 "JokeDate=CURDATE()";
	  if (mysql_query($sql)) {
		echo("<P>Your joke has been added.</P>");
	  } else {
		echo("<P>Error adding submitted joke: " .
			 mysql_error() . "</P>");
	  }
	}
  
  
echo ("<P> Here are all the jokes in our database: </P>
<BLOCKQUOTE>");

  
  
  
  // Request the text of all the jokes
  $result = mysql_query(
			"SELECT JokeText FROM Jokes");
  if (!$result) {
	echo("<P>Error performing query: " .
		 mysql_error() . "</P>");
	exit();
  }

  // Display the text of each joke in a paragraph
  while ( $row = mysql_fetch_array($result) ) {
	echo("<P>" . $row["JokeText"] . "</P>");
  }

 echo("</BLOCKQUOTE>");
 
// When clicked, this link will load this page
	// with the joke submission form displayed.
	echo("<P><A HREF='$_SERVER[PHP_SELF]?addjoke=1'>" .
		 "Add a Joke!</A></P>");
 
 ?>
 
 <?php endif; ?>


</BODY>
</HTML>



The link now loads the form as required. Initially, upon loading the page I get an error: "Notice: Undefined index: submitjoke." Plus when I submit a joke, the page reasures me that my joke has been added, yet upon checking the database the script has inserted a blank record?? :)

any ideas please? :)

Attached image(s)

  • Attached Image
  • Attached Image

This post has been edited by 1cookie: 14 September 2009 - 06:42 AM

Was This Post Helpful? 0
  • +
  • -

#6 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Undefined variable/index

Posted 14 September 2009 - 09:11 AM

You need to assign the variable $joketext the value of $_POST['joketext'] Thats the reason for the notice and blank entry ...

  // If a joke has been submitted,
	// add it to the database.
	if ("SUBMIT" == $_POST['submitjoke']) {
	  $joketext = $_POST['joketext'];
	  $sql = "INSERT INTO Jokes SET " .
			 "JokeText='$joketext', " .
			 "JokeDate=CURDATE()";
	  if (mysql_query($sql)) {
		echo("<P>Your joke has been added.</P>");
	  } else {
		echo("<P>Error adding submitted joke: " .
			 mysql_error() . "</P>");
	  }
	}


This post has been edited by RPGonzo: 14 September 2009 - 09:13 AM

Was This Post Helpful? 0
  • +
  • -

#7 1cookie  Icon User is offline

  • D.I.C Regular

Reputation: -5
  • View blog
  • Posts: 338
  • Joined: 19-October 06

Re: Undefined variable/index

Posted 14 September 2009 - 09:49 AM

View PostRPGonzo, on 14 Sep, 2009 - 08:11 AM, said:

You need to assign the variable $joketext the value of $_POST['joketext'] Thats the reason for the notice and blank entry ...


Thanks, that's sorted the update database issue. :) One more thing though, upon loading the page i get an "Notice: Undefined index: submitjoke, on line 39"??

  
	if ("SUBMIT" == $_POST['submitjoke']) { //line 39
	



how do i suppress this? :)
Was This Post Helpful? 0
  • +
  • -

#8 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Undefined variable/index

Posted 14 September 2009 - 10:04 AM

Need to check it for being set before checking the value ..

if (isset($_POST['submitjoke']) && $_POST['submitjoke'] == "SUBMIT") {


Was This Post Helpful? 1
  • +
  • -

#9 1cookie  Icon User is offline

  • D.I.C Regular

Reputation: -5
  • View blog
  • Posts: 338
  • Joined: 19-October 06

Re: Undefined variable/index

Posted 14 September 2009 - 10:18 AM

View PostRPGonzo, on 14 Sep, 2009 - 09:04 AM, said:

Need to check it for being set before checking the value ..

if (isset($_POST['submitjoke']) && $_POST['submitjoke'] == "SUBMIT") {




...Fantastic! :) Many thanks...
Was This Post Helpful? 0
  • +
  • -

#10 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Undefined variable/index

Posted 14 September 2009 - 10:19 AM

no problem :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1