can someone tell me what is wrong with this code

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
kulikedat
Forum Newbie
Posts: 1
Joined: Sun May 11, 2003 8:47 pm

can someone tell me what is wrong with this code

Post by kulikedat »

hi, I am using php 4.2 and this is a tutorial i got from the web, I created the database and it working I was able to input data directly using mysql. but when I tried input the data from the web nothing happened after I clicked SUBMIT. Can someone tell me what I need to fix.

Thanks
(mod edit: added

Code: Select all

tag for some readability)

Code: Select all

<HTML>
<BODY>


<?php  // If the user wants to add a joke  
$addjoke=$_GET[addjoke];
if (isset($addjoke)):
?>

<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>

<?php  
else:

    // Connect to the database server    
$dbcnx = @mysql_connect("localhost","root", "");    

if (!$dbcnx) 
{      
echo( "<P>Unable to connect to the " ."database server at this time.</P>" );      
exit();    
}


    // Select the jokes database 
mysql_select_db("jokes",$dbcnx);    

if (! @mysql_select_db("jokes") ) 
{      
echo( "<P>Unable to locate the joke " . "database at this time.</P>" );      
exit();   
 }

// If a joke has been submitted,    
$submitjoke= $_POST[submitjoke];

echo("<P> Here are all </P>");      

if ($submitjoke) 

{      



$joketext = $_POST[joketext];
echo("<P> works until here</P>");      

$sql = "INSERT INTO jokes SET " . "JokeText='$joketext', " . "JokeDate=CURDATE()";      
$result = mysql_query($sql); 

if (mysql_query($sql)) {        

echo("<P>Your joke has been added.</P>");      

} 

else 
{        

echo("<P>Error adding submitted joke: " .             

mysql_error() . "</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",$dbcnx);    

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
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

what exactly do you mean by nothing?
btw: echo($PHP_SELF); might be undefined depending on your php-version/condifuration. echo $_SERVER['PHP_SELF'] takes the place of it with register_globals off
Post Reply