making a insert fields....

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

making a insert fields....

Post by Smackie »

alright i just got up the poem part that i wanted well im making a place where the users can go to submit there poems and well i just used my signup script and edited it alil bit it works expect for adding stuff to the database can someone find what im doing wrong??

code

Code: Select all

<?php

// dbConfig.php is a file that contains your
// database connection information. This
// tutorial assumes a connection is made from
// this existing file.
include ("dbConfig.php");


// ==== Input validation and PHP dBase code ===============================
//
// ========================================================================

if ( $_GET&#1111;"op"] == "poe" )
	&#123;
	$bInputFlag = false;
	foreach ( $_POST as $field )
		&#123;
		if ($field == "")
			&#123;
			$bInputFlag = false;
			&#125;
		else
			&#123;
			$bInputFlag = true;
			&#125;
		&#125;
	// If we had problems with the input, exit with error
	if ($bInputFlag == false)
		&#123;
		die( "Problem with your registration info. "
			."Please go back and try again.");
		&#125;

	// Fields are clear, add user to database
	//  Setup query
	$q = "INSERT INTO `dbpoems` (`username`,`poem`) "
		."VALUES ('".$_POST&#1111;"username"]."', "
		."poem('".$_POST&#1111;"poem"]."'), ";
		//  Run query
	$r = mysql_query($q);
	
	// Make sure query inserted user successfully
	if ( !mysql_insert_id() )
		&#123;
		die("Error: You stink to much so you didnt get put in there =P.");
		&#125;
	else
		&#123;
		// Redirect to thank you page.
		Header("Location: poems.php");
		&#125;
	&#125; // end if


// ==== Thank you page ====================================================
// 
// ========================================================================
elseif ( $_GET&#1111;"op"] == "thanks" )
	&#123;
	echo "<h2>Thanks for submitting your poems at Haunted Graveyard!</h2>";
                &#125;
		


// ==== Main Form =========================================================
//
// ========================================================================
else
&#123;
echo "<form action="?op=poe" method="POST">\n";
echo "Username: <input name="username" MAXLENGTH="25"><br />\n";
echo "Poem: <input type="poem" name="poem" MAXLENGTH="25"><br />\n";
echo "<input type="submit">\n";
echo "</form>\n";
	&#125;
// EOF
?>
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Hi,

I think there is a problem with your query. After the assignment

Code: Select all

$q = "INSERT INTO `dbpoems` (`username`,`poem`) "
      ."VALUES ('".$_POST&#1111;"username"]."', "
      ."poem('".$_POST&#1111;"poem"]."'), ";
the query would be something like, for example,

Code: Select all

"INSERT INTO `dbpoems` (`username`,`poem`) VALUES ('orlando', poem('bla bla bla'), "
which doesn't seem to be a valid query. You call toa function poem() and procedures are supported only on MySQL 5.0. Also you have an ending , which should be replaced by the closing ).

What is the error message if you're getting some? What is the description of the dbpoems table? What is the type the poem filed? Think you might remove that poem() function and insert the poem itself.

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

Post by Smackie »

See i aint getting no error.. i pulled almost that whole script out of my signup script that i made it works just fine until i try sending in the user name and poem it just goes right to the "oops you didnt get put in the database sorry"... and i dont see nothing wrong in working with it that way :?: .....
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Hmmm... let us see what error is raised by MySQL. Change the line where you send the query like this:

Code: Select all

$r = mysql_query($q) or die('MySQL error: ' . mysql_error() . '<br>Query: ' . $q);
Then show us the error. Also, what is the type of the poem field?

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

Post by Smackie »

MySQL error: Query was empty
Query:
thats the error i got..

the poem on is VARCHAR i think...

is there someway i can get more then 255 letters put into there so people can add there longer poems in there as well?
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Strange you're geting an empty query :o Are you sure you correctly changed the line?

So, VARCHAR, ok. What kind of data is a poem in your application? Is it just text, line by line? I'm asking this to know if it can be store into that field.
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

yeah i think its text line by line im not for sure im somewhat of a newbie...
but what i posted was the full page for that....


?? :?: is there someway i can get more then 255 letters put into there so people can add there longer poems in there as well :?: ??
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you don't use VARCHAR to have longer text.. TEXT, MEDIUMTEXT, LONGTEXT are for those things.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Smackie wrote:(...) ?? :?: is there someway i can get more then 255 letters put into there so people can add there longer poems in there as well :?: ??
Ok, ok, I saw it on the last post. It seems a field of type TEXT is the best option for you. Reference: MySQL Reference Manual :: 11.4.3 The BLOB and TEXT Types

Also, what you get if you echo the query (echo $q) after the line you declare it?
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

what do you mean by
Also, what you get if you echo the query (echo $q) after the line you declare it?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

echo the variable $q after you set it. Tell us what you see.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

I mean, you're getting an empty query error message from MySQL. Just print the query right after you set it up. It is a kind of debugging. So just place something like this in your code:

Code: Select all

// Fields are clear, add user to database
   //  Setup query
   $q = "INSERT INTO `dbpoems` (`username`,`poem`) "
      ."VALUES ('".$_POST&#1111;"username"]."', "
      ."poem('".$_POST&#1111;"poem"]."'), ";

   //  See what the query looks like, comment to have the normal execution of the script
   die($q);
[edit]
feyd, you're too fast, man :lol:
[/edit]
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

now im just getting

Code: Select all

INSERT INTO `dbpoems` (`username`,`poem`) VALUES ('Smackie', poem('testing'),

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

Post by feyd »

well.. you're missing a closing paren .. if you don't have a stored procedure or function called 'poem' then you may get an error with that too..
Post Reply