Undefined Variable Following MySQL Tutorial

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
richo
Forum Commoner
Posts: 58
Joined: Sun Aug 06, 2006 11:56 am

Undefined Variable Following MySQL Tutorial

Post by richo »

Here is my code. It comes up as undefined variable on $submitgig, $gigtitle and $gigdate when i'm using them in the form. What can i do? Any help much appreciated!

Code: Select all

/* Add new gig if submitted */

if ("submit" == $submitgig) {
  $sql = "INSERT INTO gigdates SET " .
         "gigtitle='$gigtitle', " .
         "gigdate='$gigdate'";
  if (mysql_query($sql)) {
    echo("<p>The gig has been added.</p>");
  } else {
    echo("<p>Error adding adding gig: " .
         mysql_error() . "</p>");
  }
}

?>

<ul>
<?php
while ( $row = mysql_fetch_array($result) ) {
  echo("<li><a href= \" ../news.php \" >" . $row["gigtitle"] . "</a></li>");
  echo("<li>" . $row["gigdate"] . "</li>");
}
?>
</ul>

<form action="<?php echo($PHP_SELF); ?>" method=post>
<p>Input where you're playing (eg: The Vine, Leeds):</p>
<input type="text" name="gigtitle"></input>
<p>Input the date of where you're playing (eg:17th August 2007):</p>
<input type="text" name="gigdate"></input>
<p><input type=submit name="submitgig" value="submit"></p>
</form>
richo
Forum Commoner
Posts: 58
Joined: Sun Aug 06, 2006 11:56 am

Post by richo »

Doesn't this only hide the errors and not solve the problem?

ie, it stops the error from coming up but my code still doesn't work?

the reply i was responding to seems to have disappeared
Last edited by richo on Sun Aug 06, 2006 12:11 pm, edited 1 time in total.
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

I'd guess that you're working with bit outdated tutorial that required register_globals to be turned on.
What you need to do is initialize your variables first, for example

Code: Select all

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

  $gigtitle = $_POST['gigtitle'];
  $gigdate = $_POST['gigdate'];

  $sql = "INSERT INTO gigdates SET " .
         "gigtitle='$gigtitle', " .
         "gigdate='$gigdate'";
  if (mysql_query($sql)) {
    echo("<p>The gig has been added.</p>");
  } else {
    echo("<p>Error adding adding gig: " .
         mysql_error() . "</p>");
  }
}

?>

<ul>
<?php
while ( $row = mysql_fetch_array($result) ) {
  echo("<li><a href= \" ../news.php \" >" . $row["gigtitle"] . "</a></li>");
  echo("<li>" . $row["gigdate"] . "</li>");
}
?>
</ul>

<form action="<?php echo($PHP_SELF); ?>" method=post>
<p>Input where you're playing (eg: The Vine, Leeds):</p>
<input type="text" name="gigtitle"></input>
<p>Input the date of where you're playing (eg:17th August 2007):</p>
<input type="text" name="gigdate"></input>
<p><input type=submit name="submitgig" value="submit"></p>
</form>
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

Hello,

Change the top line (the if) to:

Code: Select all

if ($_POST['submitgig']) {
And you are relying on register globals really heavily, which isnt great for security. I reccommend putting this right after the first if:

Code: Select all

$gigtitle = mysql_real_escape_string($_POST['gigtitle']);
$gigdate = mysql_real_escape_string($_POST['gigdate']);
(The mysql_real_escape_strings are for security)
richo
Forum Commoner
Posts: 58
Joined: Sun Aug 06, 2006 11:56 am

Post by richo »

thanks very much, that looks good, i'll it out.

So when a variable is being used to POST data, you always have to declare that is what it's for?
richo
Forum Commoner
Posts: 58
Joined: Sun Aug 06, 2006 11:56 am

Post by richo »

Okay the first one i tried from wtf didn't seem to work, it just went to a The page cannot be displayed after strangely adding in something a couple of times, here is the code:


Code: Select all

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 

	  $gigtitle = $_POST['gigtitle'];
	  $gigdate = $_POST['gigdate']; 
	  
	  $sql = "INSERT INTO gigdates SET " .
			 "gigtitle='$gigtitle', " .
			 "gigdate='$gigdate'";
	  if (mysql_query($sql)) {
		echo("<p>The gig has been added.</p>");
	  } else {
		echo("<p>Error adding adding gig: " .
			 mysql_error() . "</p>");
	  }
	}

?>

<ul>
<?php
while ( $row = mysql_fetch_array($result) ) {
  echo("<li><a href= \" ../news.php \" >" . $row["gigtitle"] . "</a></li>");
  echo("<li>" . $row["gigdate"] . "</li>");
}
?>
</ul>

<form action="<?php echo($PHP_SELF); ?>" method=post>
<p>Input where you're playing (eg: The Vine, Leeds):</p>
<input type="text" name="gigtitle"></input>
<p>Input the date of where you're playing (eg:17th August 2007):</p>
<input type="text" name="gigdate"></input>
<p><input type=submit name="submitgig" value="submit"></p>
</form>
The 2nd one from LiveFree doesn't seem to work either but i wasn't fully sure what you meant me to put in where, could you copy out the full code and change the bits????
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

Code: Select all

if ($_POST['submitgig']) {

          $gigtitle = $_POST['gigtitle'];
          $gigdate = $_POST['gigdate'];
          
          $sql = "INSERT INTO gigdates SET " .
                         "gigtitle='$gigtitle', " .
                         "gigdate='$gigdate'";
          if (mysql_query($sql)) {
                echo("<p>The gig has been added.</p>");
          } else {
                echo("<p>Error adding adding gig: " .
                         mysql_error() . "</p>");
          }
        }

?>

<ul>
<?php
while ( $row = mysql_fetch_array($result) ) {
  echo("<li><a href= \" ../news.php \" >" . $row["gigtitle"] . "</a></li>");
  echo("<li>" . $row["gigdate"] . "</li>");
}
?>
</ul>

<form method=post>
<p>Input where you're playing (eg: The Vine, Leeds):</p>
<input type="text" name="gigtitle"></input>
<p>Input the date of where you're playing (eg:17th August 2007):</p>
<input type="text" name="gigdate"></input>
<p><input type=submit name="submitgig" value="submit"></p>
</form>
richo
Forum Commoner
Posts: 58
Joined: Sun Aug 06, 2006 11:56 am

Post by richo »

Thanks liveFree but after doing that i now get:

Notice: Undefined index: submitgig in c:\Inetpub\wwwroot\theisles\temp\gigsright.php on line 22
richo
Forum Commoner
Posts: 58
Joined: Sun Aug 06, 2006 11:56 am

Post by richo »

i've realised that if i remove the:

Code: Select all

action="<?php echo($PHP_SELF); ?>"
it now works...sort of.

Not ideal as when it comes up again, the gig doesn't show until you've refreshed (in which case the form data will be re-sent).....not sure what to do about this?
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

Code: Select all

if ($_POST['submitgig']) {

          $gigtitle = $_POST['gigtitle'];
          $gigdate = $_POST['gigdate'];
         
          $sql = "INSERT INTO gigdates SET " .
                         "gigtitle='$gigtitle', " .
                         "gigdate='$gigdate'";
          if (mysql_query($sql)) {
                echo("<p>The gig has been added.</p>");
          } else {
                echo("<p>Error adding adding gig: " .
                         mysql_error() . "</p>");
          }
        }

?>

<ul>
<?php
while ( $row = mysql_fetch_array($result) ) {
  echo("<li><a href= \" ../news.php \" >" . $row["gigtitle"] . "</a></li>");
  echo("<li>" . $row["gigdate"] . "</li>");
}
?>
</ul>

<form action="<?php echo $_SERVER['PHP_SELF']; ?> " method=post>
<p>Input where you're playing (eg: The Vine, Leeds):</p>
<input type="text" name="gigtitle"></input>
<p>Input the date of where you're playing (eg:17th August 2007):</p>
<input type="text" name="gigdate"></input>
<p><input type=submit name="submitgig" value="submit"></p>
</form>
Where are you getting the $result var?
richo
Forum Commoner
Posts: 58
Joined: Sun Aug 06, 2006 11:56 am

Post by richo »

Okay, cool, that adds it and comes up with the desired "The gig has been added."

But displays the same list as it did before pressing submit despite it being added to the table.

You can only see the new list by refreshing (but this would re-add data) or going somewhere and coming back.

Is there a way of solving this?

The $result comes from here:


Code: Select all

$result = mysql_query("SELECT * FROM gigdates");
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

Code: Select all

if ($_POST['submitgig']) {

          $gigtitle = $_POST['gigtitle'];
          $gigdate = $_POST['gigdate'];
         
          $sql = "INSERT INTO gigdates SET " .
                         "gigtitle='$gigtitle', " .
                         "gigdate='$gigdate'";
          if (mysql_query($sql)) {
                echo("<p>The gig has been added.</p>");
          } else {
                echo("<p>Error adding adding gig: " .
                         mysql_error() . "</p>");
          }
         ?>
<ul>
<?php
while ( $row = mysql_fetch_array($result) ) {
  echo("<li><a href= \" ../news.php \" >" . $row["gigtitle"] . "</a></li>");
  echo("<li>" . $row["gigdate"] . "</li>");
}
?>
</ul>
<?php
        }

?>


<form action="<?php echo $_SERVER['PHP_SELF']; ?> " method=post>
<p>Input where you're playing (eg: The Vine, Leeds):</p>
<input type="text" name="gigtitle"></input>
<p>Input the date of where you're playing (eg:17th August 2007):</p>
<input type="text" name="gigdate"></input>
<p><input type=submit name="submitgig" value="submit"></p>
</form>
Try that
richo
Forum Commoner
Posts: 58
Joined: Sun Aug 06, 2006 11:56 am

Post by richo »

that script errors.

I think that if i post to another page and then have a re-direct or link back, that would solve the problem, even if it is a cowards way out.

Are there any advantages for having it post back to itself?

thanks for all your help liveFree :)
richo
Forum Commoner
Posts: 58
Joined: Sun Aug 06, 2006 11:56 am

Post by richo »

in the end i posted the data to a seperate php page which inserted it and then re-directed back.

Problem solved.

Thanks for all the help.
Post Reply