Post Form to Same Page - Not Displaying Newly Added Data

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

Post Form to Same Page - Not Displaying Newly Added Data

Post by richo »

Hi,

My code adds data to the table successfully on submit. However, it won't display the newly added data (pulled out with a mySQL statement etc) until i either refresh the page or go to another page and come back.

I can solve the problem by posting to another page with a re-direct back but this is not preferable to posting the data to itself.

Has anyone had this problem before? Please help, my code is below:

Code: Select all

$result = mysql_query("SELECT * FROM gigdates");
if (!$result) {
  echo("<P>Error performing query: " .
       mysql_error() . "</P>");
  exit();
}

/* Add new gig if submitted */

if (isset($_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 $_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>
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

You get the $result before adding the new data.
richo
Forum Commoner
Posts: 58
Joined: Sun Aug 06, 2006 11:56 am

Post by richo »

MANY THANKS!

A simple but effective solution! It's been confusing me for many hours!!!

cheers.
Post Reply