Page 1 of 1

text not being submitted to database

Posted: Tue Feb 11, 2003 5:37 pm
by Purdue23
I am having trouble with the text being added to the database. When I submit the form, the curdate() gets submitted but not the text entered into the text area. Any suggestions?

Thanks



<html>
<head>
<title> The Internet Joke Database </title>
</head>
<body>

<?php
if (isset($_GET['addjoke'])):?>

<form action="<?php echo($_SERVER['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" /></p>
</form>


<?php
else: // Default page display
// Connect to the database server
$dbcnx = @mysql_connect("localhost", "", "");
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();
}

if (isset($_POST['joketext'])){ // check to see if a joke has been posted
$sql = "INSERT INTO Jokes SET JokeText='$joketext', JokeDate=CURDATE()";
if( mysql_query($sql,$dbcnx)){
echo("<p>Your joke has been added.</p>");
}

}
echo("<p> Here are all the jokes in our database: </p>");

// 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>");
}

// When clicked, this link will load this page
// with the joke submission form displayed.

echo("<p><a href=\"".$PHP_SELF."?addjoke=1\">Add a Joke!</a></p>");



endif;

?>

</body>
</html>

Posted: Tue Feb 11, 2003 5:46 pm
by skehoe
[quote]
if (isset($_POST['joketext'])){ // check to see if a joke has been posted
$sql = "INSERT INTO Jokes SET JokeText='$joketext', JokeDate=CURDATE()";
if( mysql_query($sql,$dbcnx)){
echo("<p>Your joke has been added.</p>");
}
[/quote]

Try adding this line right above your insert:

$joketext = $_POST['joketext'];

Hope that helps.

Thanks

Posted: Tue Feb 11, 2003 5:54 pm
by Purdue23
That worked! Would you also happen to know how I could get the text area to open up on the same page. I have tried to use _post but have not had any luck.

<html>
<head>
<title> The Internet Joke Database </title>
</head>
<body>

<?php
if (isset($_GET['addjoke'])):?>

<form action="<?php echo($_SERVER['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" /></p>
</form>

<?php
else: // Default page display
// Connect to the database server
$dbcnx = @mysql_connect("localhost", "", "");
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();
}

if (isset($_POST['joketext'])){ // check to see if a joke has been posted
$joketext = $_POST['joketext'];
$sql = "INSERT INTO Jokes SET JokeText= '$joketext', JokeDate=CURDATE()";
if( mysql_query($sql,$dbcnx)){
echo("<p>Your joke has been added.</p>");
}

}


echo("<p> Here are all the jokes in our database: </p>");

// 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>");
}

// When clicked, this link will load this page
// with the joke submission form displayed.

echo("<p><a href=\"".$PHP_SELF."?addjoke=1\">Add a Joke!</a></p>");



endif;

?>

</body>
</html>

Posted: Wed Feb 12, 2003 2:19 am
by twigletmac