[sf-lug] PHP script advice

Owen Pietrokowsky opietro at yahoo.com
Mon Jun 9 11:55:27 PDT 2008


Greetings,


I'm learning PHP from a tutorial, and I'm unable to get the
following code to work (The code inserts a record into a MySQL database of jokes.):


-------------------------------------------------

// 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 submitted joke: " .
             mysql_error() . "</P>");

-----------------------------------------------------


I'm using mysql-server-5.0, php5-mysql, and Dapper Drake Ubuntu 6.06. 
I've given my mysql database files complete permissions, and checked the php 
and mysql config files. I've also granted all privileges for the
database on root at localhost. Nonetheless, the code listed above doesn't
work, and I don't get any echoed confirmations or errors. Any advice
would be much appreciated. Here is the complete code for the PHP script:


-----------  Complete PHP script -------------------

<HTML>
...
<BODY>
<?php
  // If the user wants to add a joke
  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", "mypasswd");
    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 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 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");
    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>

---------------------------------------------------



Owen Pietrokowsky
opietro at yahoo.com



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://linuxmafia.com/pipermail/sf-lug/attachments/20080609/c9301c9d/attachment.html>


More information about the sf-lug mailing list