z99tms Posted February 16, 2007 Share Posted February 16, 2007 hi, i've been following along with the MYSQL homepage's tutorial on getting a basic Joke database working via PHP. Everything went very well and I've found it to be excellent, but I can't get the database to take info from a form. I've got a form there where I can enter in the new joke, a button which says submit, but nothing gets added? I'd really appreciate some advice on this - I'm so close to getting something functional with this! --------------------------------------------------------- <HTML> <HEAD> <TITLE> Our List of Jokes </TITLE> <HEAD> <BODY> <?php // Connect to the database server $dbcnx = @mysql_connect("localhost", "root", "root"); if (!$dbcnx) { echo( "<P>Unable to connect to the " . "database server at this time.</P>" ); exit(); } // Select the jokes database if (! @mysql_select_db("jokes") ) { echo( "<P>Unable to locate the joke " . "database at this time.</P>" ); exit(); } ?> <P> Here are all the jokes in our database: </P> <BLOCKQUOTE> <?php // Request the text of all the jokes $result = mysql_query( "SELECT JokeText FROM Jokes"); if (!$result) { echo("<P>Error performing query: " . mysql_error() . "</P>"); exit(); } // Display the text of each joke in a paragraph while ( $row = mysql_fetch_array($result) ) { echo("<P>" . $row["JokeText"] . "</P>"); } if ("SUBMIT" == $submitjoke) { $sql = "INSERT INTO Jokes SET " . "JokeText='$joketext', " . "JokeDate=CURDATE()"; if (mysql_query($sql)) { echo("<P>Your joke has been added.</P>"); } else { echo("<P>Error adding submitted joke: " . mysql_error() . "</P>"); } } ?> <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST> <P>Type your joke here:<BR> <TEXTAREA NAME="joketext" ROWS=10 COLS=40 WRAP></TEXTAREA><BR> <INPUT TYPE=SUBMIT NAME="submitjoke" VALUE="SUBMIT"> </FORM> </BLOCKQUOTE> </BODY> </HTML> Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted February 16, 2007 Share Posted February 16, 2007 try replacing this part: if ("SUBMIT" == $submitjoke) { $sql = "INSERT INTO Jokes SET " . "JokeText='$joketext', " . "JokeDate=CURDATE()"; if (mysql_query($sql)) { echo("<P>Your joke has been added.</P>"); } else { echo("<P>Error adding submitted joke: " . mysql_error() . "</P>"); } } with this: if (isset($_POST['submit'])) { $sql = "INSERT INTO Jokes (`JokeText`,`JokeDate`) VALUES ('".$_POST['joketext']."', now())"; if (mysql_query($sql)) { echo "<P>Your joke has been added.</P>"; } else { echo "<P>Error adding submitted joke:".mysql_error() . "</P>"; } } Quote Link to comment Share on other sites More sharing options...
printf Posted February 16, 2007 Share Posted February 16, 2007 Change this... if ("SUBMIT" == $submitjoke) { $sql = "INSERT INTO Jokes SET " . "JokeText='$joketext', " . "JokeDate=CURDATE()"; if (mysql_query($sql)) { echo("<P>Your joke has been added.</P>"); } else { echo("<P>Error adding submitted joke: " . mysql_error() . "</P>"); } } ?> <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST> <P>Type your joke here:<BR> <TEXTAREA NAME="joketext" ROWS=10 COLS=40 WRAP></TEXTAREA><BR> <INPUT TYPE=SUBMIT NAME="submitjoke" VALUE="SUBMIT"> </FORM> </BLOCKQUOTE> </BODY> </HTML> To something like... if ( ! empty ( $_POST['send'] ) && ! empty ( $_POST['joketext'] ) ) { $text_test = trim ( $_POST['joketext'] ); if ( ! empty ( $text_test ) ) { mysql_query ( "INSERT INTO Jokes VALUES ( '" . mysql_real_escape_string ( $text_test ) . "', CURDATE() );" ); if ( mysql_affected_rows () == 1 ) { echo "<p>Your joke has been added.</p>"; } else { echo "<p>Error adding submitted joke: " . mysql_error () . "</p>"; } } } ?> <FORM ACTION="<?php echo $_SERVER['PHP_SELF'];?>" METHOD=POST> <input type='hidden' name='send' value='1'> <P>Type your joke here:<BR> <TEXTAREA NAME="joketext" ROWS=10 COLS=40 WRAP></TEXTAREA><BR> <INPUT TYPE=SUBMIT NAME="submitjoke" VALUE="SUBMIT"> </FORM> </BLOCKQUOTE> </BODY> </HTML> But the query format depends on your database table format. The problem you are having is a registered global issue. You must validate your form inputs using one of the newer SUPER GLOBALS ($_POST, $_GET, $_REQUEST) Quote Link to comment Share on other sites More sharing options...
z99tms Posted February 16, 2007 Author Share Posted February 16, 2007 Humm....those didn't work for some reason. I don't get an error or a new joke? Thanks for this again. Quote Link to comment Share on other sites More sharing options...
z99tms Posted February 16, 2007 Author Share Posted February 16, 2007 Its adding new blank entries into the database, but not adding actual text. hUmmm. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted February 17, 2007 Share Posted February 17, 2007 try this then. to see if your actually getting values. <HTML> <HEAD> <TITLE> Our List of Jokes </TITLE> <HEAD> <BODY> <?php // Connect to the database server $dbcnx = @mysql_connect("localhost", "root", "root"); if (!$dbcnx) { echo( "<P>Unable to connect to the " . "database server at this time.</P>" ); exit(); } // Select the jokes database if (! @mysql_select_db("jokes") ) { echo( "<P>Unable to locate the joke " . "database at this time.</P>" ); exit(); } ?> <P> Here are all the jokes in our database: </P> <BLOCKQUOTE> <?php // Request the text of all the jokes $result = mysql_query("SELECT * FROM Jokes"); if (!$result) { echo "<P>Error performing query:".mysql_error() . "</P>"; exit(); } // Display the text of each joke in a paragraph while ( $row = mysql_fetch_array($result) ) { echo "<P>" . $row['JokeText'] . "</P>"; } if(isset($_POST['submit'])){ if($_POST['joketext'] == ""){ echo "No data submitted!"; }else{ $sql = "INSERT INTO Jokes (`JokeText`,`JokeDate`) VALUES ('{$_POST['joketext']}', now())"; if(mysql_query($sql)) { echo "<P>Your joke has been added.</P>"; }else{ echo "<P>Error adding submitted joke:".mysql_error() . "</P>"; } } } ?> <FORM ACTION="<?php echo $_SERVER['REQUEST_URI']; ?>" METHOD=POST> <P>Type your joke here:<BR> <TEXTAREA NAME="joketext" ROWS=10 COLS=40 WRAP></TEXTAREA><BR> <INPUT TYPE=SUBMIT NAME="submitjoke" VALUE="SUBMIT"> </FORM> </BLOCKQUOTE> </BODY> </HTML> Quote Link to comment Share on other sites More sharing options...
Greaser9780 Posted February 17, 2007 Share Posted February 17, 2007 Also in the form portion where it says textarea you didn't include value="joketext" Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted February 17, 2007 Share Posted February 17, 2007 Also in the form portion where it says textarea you didn't include value="joketext" what? Quote Link to comment Share on other sites More sharing options...
Greaser9780 Posted February 17, 2007 Share Posted February 17, 2007 Like the <input has value="blah". Doesn't there need to be one for the textarea? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted February 17, 2007 Share Posted February 17, 2007 textarea dosnt have a value parameter, if you going to specify a value for textarea it needs to be like this: <textarea>A value in here</textarea> but you dont need to specify a value for it, unless you want a default value. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.