-
I've been readint he tutorial y'all have on this site...it's great.
Anyway, to "learn" this stuff, I try tinkering with scripts, but it won't work for some reason. It displays the jokes, but for some reason, when I tell it to add a joke, it won't. Here's the code I've been trying to use. (Can someone also explain to me how 'if("ADD" == $submitjoke){' is supposed to work...I don't understand how that is triggered when the user clicks on the button and submits the form .... maybe I M just to used to JavaScript, but I want to UNDERSTAND the language, so please help. (since it displays the info fromt he database, I know that it is connecting.)
<html>
<head>
<title>Today's Date</title>
</head>
<body>
<?php
$dbcnx = @mysql_connect("localhost","root","");
mysql_select_db("jokes");
?>
<h1>Add a Joke:</h1><br>
<form method="post" action="<?php echo($PHP_SELF);?>">
<textarea name="JokeText">Type your joke here:</textarea><br>
<input type="submit" name="submitjoke" value="ADD">
</form><br><Br>
<?php
if("ADD" == $submitjoke){
$sql="INSERT INTO Jokes SET".
"JokeText='JokeText', ".
"JokeDate=CURDATE()";
}
mysql_query($sql)
?>
<h1>Jokes thus far:</h1>
<?php
$jokes = mysql_query("SELECT JokeText FROM Jokes");
while ($row=mysql_fetch_array($jokes)){
echo("<P>" . $row["JokeText"]."</p>");
}
?>
</body>
</html>
-
if("ADD" == $submitjoke) means "if the variable submitjoke is equal to ADD then..." in other words, this like checks if the user has pressed the submit button or if s/he has simply entered the url. I like to do it the following way (which I think makes more sense):
echo "
<from action='". $PHP_SELF ."' method='post'>
<input type='hidden' name='submit' value='1'>
...
...
...
</form>";
if($submit) {
// do stuiff
}
----
The variable named $submit will only contain the value 1 if the form has been submitted. In php, 1 = Boolean true, so the if statement will only execute if the form has been submitted.
Hope this helps.
Christian
-
I see, now that makes more sense...thanx!
ALso, anyone know how come the textarea isn't adding any stuff to the database, like it should?
-
I dunno, but I was reading tutorial at sitepoint in conjuction with this one, and tried this code:
if($submitjoke){
$sql="INSERT INTO Jokes (JokeText,JokeDate) VALUES ('$JokeText',CURDATE())";
}
and it worked .. dunno why other one didn't http://www.SitePoint.com/forums/confused.gif
-
SQL used to UPDATE a record:
UDPATE joke_db SET joke='Knock, Knock', author="Me" where joke_id='007'
SQL used to INSERT a NEW record:
INSERT INTO joke_db (joke_id, joke, author) VALUES ('', 'Knock, Knock', 'Me')
Christian
-
Sounds like Arielladog is using a version of MySQL prior to 3.22.10, which doesn't support the newer INSERT INTO table SET ... syntax.
------------------
-Kevin Yank.
http://www.SitePoint.com/
Helping Small Business Grow Online!
-
>Sounds like Arielladog is using a version
>of MySQL prior to 3.22.10, which doesn't
>support the newer INSERT INTO table
>SET ... syntax.
Yeah... that one kept me going circles for an hour. Thanks!
I'm going to fire off an e-mail to my host and see if they are going to update the software.
elm