Form posting problem

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
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Form posting problem

Post by Smackie »

I am having a problem in a form it wont submit the data to the database... i dont get any errors.. here is the script

Code: Select all

<?php
ob_start();

if ((isset($_GET['poet_poet_id']) == true) && (isset($_Get['poemname']) == true) && (isset($_GET['poem']) == true)) 
{ 

// Tell the user it has been submitted (optional) 
echo('Your comment has been posted.');

// Set Mysql Variables
$host = 'localhost'; 
$user = 'haunted_smackie';
$pass = '*********';
$db = 'haunted_poetry';
$tb = 'poems';

// Set global variables to easier names
$add_all = "INSERT into poems (poet_id, poemname, poem) values ('$poetid', '$poemname','$poem')";
$poem_id = $_SESSION['poet_id'];
$poemname = $_GET['poemname']; 
$poem = $_GET['poem'];

// Connect to Mysql, select the correct database, and run teh query which adds the data gathered from the form into the database
mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$add_all = "INSERT into poems (poet_id, poemname, poem) values ('$poetid', '$poemname','$poem')";

mysql_query($add_all) or die(mysql_error()); 
}
else
{
echo $add_all;
// If the form has not been submitted, display it!
?>
<form method='get' action='<? echo $PHP_SELF; ?>'>
Poem Name: <input type='text' name='poemname'><br><br>
Poem: <br>
<textarea name='poem' cols='50' rows='10'></textarea><br><br>
<input type='submit' value='Post your poem'>
</form>  
<?
}
?>

feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Last edited by Smackie on Sat Mar 19, 2005 11:52 pm, edited 1 time in total.
thegreatone2176
Forum Contributor
Posts: 102
Joined: Sun Jul 11, 2004 1:27 pm

Post by thegreatone2176 »

two things first

1) edit your password out on public forums
2) global variables are bad to use as im sure everyone else will post about

also
i dont think your if will be true ever because where do you ever get
$_GET['poet_poet_id']

and you have

Code: Select all

if ((isset($_GET['poet_poet_id']) == true)
you can just do

Code: Select all

if (isset($_GET['poet_poet_id']))
last whats your final
<?
}
?>
about ?
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

it was suppose to be $_GET['poet_id'] but see i have 2 tables in db 1 is for the login and the other is for the id, poem, and poem name and poet_id should link the two tables together.......
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

this
<?
}
?> just to show that the script is done thats all it shouldnt hurt the script any i dont think it hasnt messed up my other scripts that i have done.....
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post by thallish »

hey

first off all then the statement in line 18 is unnecessary. You have it in line 26.

In line 19 you set the variable $poem_id but in line 26 you use a variable in the variable $poetid as a value. I believe that's the problem.

regards thallish
Last edited by thallish on Sun Mar 20, 2005 9:10 am, edited 1 time in total.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

If that doesn't fix it, but I think it will, echo out your query to the screen then run it directly agianst your db and see if it spits out any errors at you, could be simple spelling or wront column name.

phpscott
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

poet_poet_id, poet_id, poem_id, and $poetid may all be potential sources of confusion about this.

Sanitize your inputs. You don't know where they've been ;)
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

yeah i seen that i did that i fixed them but its still not posting anything :(
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post by thallish »

can you post your editted code then :wink:

thallish
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

Code: Select all

<?php
session_start();

if (!$_SESSION["valid_user"])
	{
	// User not logged in, redirect to login page
	Header("Location: http://www.hauntedgraveyard.net/Poetry/ ... /login.php");
	}
if ((isset($_SESSION['poet_id'])) && (isset($_Get['poemname']) == true) && (isset($_GET['poem']) == true)) 
{ 

// Tell the user it has been submitted (optional) 
echo('Your comment has been posted.');

// Set Mysql Variables
$host = 'localhost'; 
$user = 'haunted_smackie';
$pass = '*****';
$db = 'haunted_poetry';
$tb = 'poems';

// Set global variables to easier names
$poet_id = $_SESSION['poet_id'];
$poemname = $_GET['poemname']; 
$poem = $_GET['poem'];

// Connect to Mysql, select the correct database, and run teh query which adds the data gathered from the form into the database
mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$add_all = "INSERT into poems (poemname, poem) values ('$poemname','$poem')";

mysql_query($add_all) or die(mysql_error()); 
}
else
{
echo $add_all;
// If the form has not been submitted, display it!
?>
<form method='get' action='<? echo $PHP_SELF; ?>'>
Poem Name: <input type='text' name='poemname'><br><br>
Poem: <br>
<textarea name='poem' cols='50' rows='10'></textarea><br><br>
<input type='submit' value='Post your poem'>
</form>  
<?
}
?>

feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

thegreatone2176 wrote: last whats your final
<?
}
?>
about ?
that ends his "ELSE" statement...



Anyways, try this. I've added comments where I changed the code...

Code: Select all

&lt;?php
session_start();
if (!$_SESSION&#1111;&quote;valid_user&quote;])
	{
		// User not logged in, redirect to login page
		Header(&quote;Location: http://www.hauntedgraveyard.net/Poetry/ ... php&quote;);
	}
//The following If(isset statement has been changed.  You had some extra () marks and unecessary == true
//statements.  Also, you should be using a $_POST instead of a $_GET as you aren't passing anything via
//a url..
if ((isset($_SESSION&#1111;'poet_id']) &amp;&amp; isset($_POST&#1111;'poemname']) &amp;&amp; isset($_POST&#1111;'poem']))
{ 
	// Tell the user it has been submitted (optional) 
	echo('Your comment has been posted.');
	// Set Mysql Variables
	$host = 'localhost'; 
	$user = 'haunted_smackie';
	$pass = 'deketazz';
	$db = 'haunted_poetry';
	$tb = 'poems';

	// Set global variables to easier names
	$poet_id = $_SESSION&#1111;'poet_id'];
	$poemname = $_GET&#1111;'poemname']; 
	$poem = $_GET&#1111;'poem'];

	// Connect to Mysql, select the correct database, and run teh query which adds the data gathered from the form into the database
	mysql_connect($host,$user,$pass) or die(mysql_error());
	mysql_select_db($db) or die(mysql_error());
//moved your entire sql statement into $add_all since you aren't doing anything else with this query anyways.
	$add_all = mysql_query(&quote;INSERT into poems (poemname, poem) values ('&quote;.$poemname.&quote;','&quote;.$poem.&quote;')&quote;) or die(MySQL_Error());
}
else
{
	//Removed your echo $add_all statement.  This is because you will NOT be able to echo out that
	//variable due to the fact that it is only defined when the IF statement rings true.  Here, you are
	//trying to say &quote;when the IF statement fails, echo what I have assigned for $add_all&quote;..leaving you with
	//nothing to be echo'ed..
//changed the Form's method to POST instead of GET.
?&gt;
&lt;form method='POST' action='&lt;? echo $PHP_SELF; ?&gt;'&gt;
Poem Name: &lt;input type='text' name='poemname'&gt;&lt;br&gt;&lt;br&gt;
Poem: &lt;br&gt;
&lt;textarea name='poem' cols='50' rows='10'&gt;&lt;/textarea&gt;&lt;br&gt;&lt;br&gt;
&lt;input type='submit' value='Post your poem'&gt;
&lt;/form&gt;  
&lt;?
}
?&gt;

hope this helps.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Smackie, start using

Code: Select all

, NOW.
Post Reply