Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SET or Values?

Status
Not open for further replies.

scitech

Technical User
Jan 6, 2005
40
GB
I'm learning slowly but I cannot see why this code does not insert into the database, The page loads fine no errors at all, should I be useing VALUES? but thats not what's in the Tutorial I'm useing

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?PHP

include 'config.php';
include 'opendb.php';
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<BR>
Type Your Joke Here:- <BR>
Joke:<INPUT NAME ="joketext" type = "text">
<BR>
<INPUT TYPE="SUBMIT" NAME="submitjoke" id="SUBMIT" VALUE="SUBMIT">
</FORM>
<?PHP
//if a joke has been submitted
//add it to the database
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 Joke:". mysql_error() ."</P>");
}
}
echo("<p>Here are all the jokes in My Database:-</p>");


//request text from all 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><BR><BR><BR>");
}
include'close.php';
?>
</body>
</html>
 
This
Code:
    $sql = "INSERT INTO Jokes SET " .
         "JokeText = $joketext, " .
         "JokeDate = CURDATE()";

will not work.

SET is normally used to UPDATE data, not to insert.
$joketext must be surrounded by single-quotes as it is a string


So it should go like

Code:
    $sql = "INSERT INTO Jokes (JokeText, JokeDate) " .
         "VALUES ('$joketext', CURDATE())";
 
No, you can use SET on an INSERT. I do it all the time and it works fine.

Ken
 
Just saw the problem....

The OP is assuming that register_globals is set to ON, it is now set to OFF. Where ever you are referencing a value that is coming from the form, use the super array $_POST to reference it. For example, this:
Code:
if("SUBMIT" == $submitjoke)
{
    $sql = "INSERT INTO Jokes SET " .
         "JokeText = $joketext, " .
         "JokeDate = CURDATE()";
should be written
Code:
if($_POST['submitjoke'] == "SUBMIT")
{
    $sql = "INSERT INTO Jokes SET " .
         "JokeText = '" . $_POST['joketext'] . "', " .
         "JokeDate = CURDATE()";

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top