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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

post not actually posting 2

Status
Not open for further replies.

frankienstien

Programmer
Apr 12, 2006
8
US
I'm trying to get this page to insert data into a mysql table but after hitting the submit button the data is not being submitted to the database.
i'm not sure what is happening. i know the fields are coming back empty but i don't know why. any hints as why this is not working correctly?
thank you

sorry if this looks like crap i'm still learning php coding.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add News Item</title>
<style type="text/css">
<!--
body {
	background-color: #CFAEF7;
}
-->
</style>
<script language="javascript" type="text/javascript" src="jscripts/tiny_mce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
  tinyMCE.init({
    theme : "advanced",
    mode: "exact",
    elements : "txtText",
	theme_advanced_toolbar_location : "top",
	plugins : "emotions",
	theme_advanced_buttons3_add : "emotions",

  });
</script>
</head>
</head>

<body>
<div align="center">
  <?php
# database connection 
<removed for my safety>


mysql_connect($dbhost,$dbuser,$dbpass) or die("could not connect");
mysql_select_db("$db") or die("could not open database");

############################################################################
#
# Table definitions

$USER_TB = 'calendar_users' ;

$calpath = dirname(__FILE__) ; 
$pos = strpos($calpath, "/");
if (!is_integer($pos)) $adpath = $calpath.'\\' ;
else 
	$calpath = $calpath."/" ;
ob_start();		// set output_buffering ON

if (!isset($_GET['op']))
  $op = '';
else
  $op = $_GET['op'];
  
if ($op == ""){echo "You must login first.";
			echo "<meta http-equiv=\"refresh\" content=\"3;url=index.php\">";
			exit;}
if ($op == "loginok"){echo "Thank you for taking the time to update us on things you like or want us know.<br />
			All entries will be reviewed by the administrator.";}

if ($op == "Insert"){$query="INSERT INTO `tblNews` ( `ID` , `Heading` , `Text` , `DateAdded` , `TimeAdded` )
			VALUES ('', '" . $txtHeader . "', '" . $txtText . "', CURDATE( ) , CURTIME( ));";
if ($txtHeader ==""){echo "Please enter a Heading for your News Item.";
			echo "<br/><br/><a href=javascript:history.back()>Back</a><br/>";
			exit;}
if ($txtText ==""){echo "Please enter a Description for your News Item.";
			echo "<br/><br/><a href=javascript:history.back()>Back</a><br/>";
			exit;}

$result=mysql_query($query);
if (!$result) {exit('<p>Error performing search: ' . mysql_error() . '</p>');}
echo "Sucess <br>";
echo "Your News Item " .$txtHeader. " Has Been Added.<br>";
echo "Retruning you to the home page.";
echo "<meta http-equiv=\"refresh\" content=\"3;url=Default.php\">";	
exit;	  
}
?>
</div>
<div align="center">
  <?php
	echo "<div align=center><form action=addnews.php?op=Insert method=post><table align=center width=300 border=0><tr><td align=right><div class=menufont>";
	echo "<b>".ucfirst(Heading).":</b></div></td><td align=left><input type=text SIZE=50 MAXLENGTH=50 name=txtHeader></td></tr><tr><td align=right><div class=menufont>" ;
	echo "<b>".ucfirst(Text).":</b></div></td><td align=left><TEXTAREA NAME=txtText COLS=50 ROWS=10></TEXTAREA></td></tr><tr><td align=left>&nbsp;</td><td align=center>" ;
	echo "<input type=submit value=Insert></td></tr>";
	echo "</table>" ;
	echo "</form>";
	echo "</div>" ;
	
if (!$txtHeading =="")
{if ($txtText ==""){echo "Please enter a Description for your News Item.";}
}
else
{echo "Please enter a Heading for your News Item.";}
?>
</div>
</body>

</html>
 
It's probably because you're writing your script like a console application.

A console application can output the fields that need to be filled in then wait for the user. When the data is ready, the program processed the data. It's all one interaction.

You're writing a web application. Any interation between your user and your script requires two interactions, one to display the HTML form and a second interaction to accept the input from the user. The process is discontinuous.

So the general structure of a one-script PHP app is something like:

[tt]
<?php-pseudocode
if (/*a field's input exists*/)
{
//process the input
}
else
{
//show the HTML form to gather input. make sure the
//form contains the field we're testing for in the
//if statement. Make sure the form submits back
//to this script
}
[/tt]



Want the best answers? Ask the best questions! TANSTAAFL!
 
In addition to Sleipnir's advice. You are assuming that Register Globals is set to on and expect $txtHeading and $txtText to exist.

When infact, Register Globals has defaulted to Off (for security reasons) for many versions of PHP. You should be accessing the information from your form using the correct superglobal variable. in your case it would be $_POST, as that is your form's method.

so $txtHeading and $txtText should be $_POST['txtHeading'] and $_POST['txtText']. Respectively.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
ok i read the one variable but i don't seem to be able to read the second variable.
Code:
echo $_POST['txtText'];
echo '<br />';
echo $_POST['txtHeading'];
echo '<br />';
returns the txtText but not txtHeading
can you tell me what i'm doing wrong?
thanx
 
The script you posted does not show an HTML form field named "txtHeading". There's one named "txtHeader", perhaps that's the one you mean?



Want the best answers? Ask the best questions! TANSTAAFL!
 
Yeah, my mistake, on that one, the post variable should reflect the name of your form elements.

So if you have a textarea who's name is txtHeader, then your post variable should be $_POST['txtHeader'].



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Yip...
Also I found a good way to debug quickly is to print your sql statement to screen. eg:
Code:
print "$query";

This has often saved me time in terms of tracking what variable is missing etc.

Reality is built on a foundation of dreams.
 
thanx for the help.
i am trying to learn from example and not examples are equal out there on the internet.
but thanx once again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top