text not being submitted to database

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

Locked
Purdue23
Forum Newbie
Posts: 7
Joined: Tue Feb 11, 2003 11:51 am

text not being submitted to database

Post 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>
User avatar
skehoe
Forum Commoner
Posts: 59
Joined: Sun Dec 22, 2002 5:57 am
Location: Denver

Post 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.
Purdue23
Forum Newbie
Posts: 7
Joined: Tue Feb 11, 2003 11:51 am

Thanks

Post 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>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Locked