Page 1 of 1

Undefined Variable Following MySQL Tutorial

Posted: Sun Aug 06, 2006 12:00 pm
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>

Posted: Sun Aug 06, 2006 12:10 pm
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

Posted: Sun Aug 06, 2006 12:10 pm
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>

Posted: Sun Aug 06, 2006 12:11 pm
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)

Posted: Sun Aug 06, 2006 12:12 pm
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?

Posted: Sun Aug 06, 2006 12:23 pm
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????

Posted: Sun Aug 06, 2006 12:50 pm
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>

Posted: Sun Aug 06, 2006 12:53 pm
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

Posted: Sun Aug 06, 2006 1:06 pm
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?

Posted: Sun Aug 06, 2006 1:20 pm
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?

Posted: Sun Aug 06, 2006 1:27 pm
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");

Posted: Sun Aug 06, 2006 1:38 pm
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

Posted: Sun Aug 06, 2006 1:44 pm
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 :)

Posted: Sun Aug 06, 2006 2:05 pm
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.