LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2004, 05:14 PM   #1
nazdrowie
Member
 
Registered: Oct 2004
Distribution: Debian
Posts: 39

Rep: Reputation: 15
More php and mysql questions


There seems to be a problem w/ the code at the bottom of the following tutorial page:

http://dev.mysql.com/tech-resources/...s/ddws/24.html

AND, I actually copied and pasted this time, replacing the 3 dots w/ the correct head and title tags at the top of the listing. Also, I corrected the variable accessing methods from say $var to $_GET[var] or $_POST[var] where necessary.

Anyway, the code displays the JokeText column of the jokes.Jokes table, but adding a joke to the db is a problem.

When the "Add a Joke!" link is clicked, we're taken to the url http://foo.org/foo.php?addjoke=1, which is fine and dandy, but when you click the submit value, we're again taken to the same url, never exiting the outer php 'if' statement, and thus never adding the joke to the db, 'cause it's the 'else' that's supposed to take care of that.

One solution is to place the form on the bottom of the initial page, so that it's always there, separate from the php logic, but I was wondering if there's another way, using which would preserve the way the app behaves: giving the add-a-joke form only when clicking on the "Add a Joke!" link, and then taking you 'back' to the page that displays the contents of the db.
 
Old 12-13-2004, 06:36 PM   #2
nazdrowie
Member
 
Registered: Oct 2004
Distribution: Debian
Posts: 39

Original Poster
Rep: Reputation: 15
Here's the code, corrected to work w/ php4 (CORRECTED as in it gives no SYNTAX errors, but the PROBLEM I outlined in my first post still EXISTS):

Code:
<HTML>
<HEAD><TITLE></TITLE></HEAD>
<BODY>
<?php
  // If the user wants to add a joke
  if (isset($_GET[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" == $_POST[submitjoke]) {
      $sql = "INSERT INTO Jokes SET " .
             "JokeText='$_POST[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>

Last edited by nazdrowie; 12-13-2004 at 08:24 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache, MySQL, PHP, a couple simple questions. The_Messiah Linux - Software 3 04-09-2005 05:17 PM
Problem getting PHP to recognize MySQL, Using PHP 4.0 and MySQL 4.0.20 d2army Programming 4 06-27-2004 08:54 PM
php4 mysql, installation, php-pages with mysql info stay empty dnla Linux - Software 2 03-14-2004 02:54 PM
Installing MySQL.4.0.17 and php support for Mysql on RedHat 9.0 Neha Linux - Software 13 12-29-2003 12:06 AM
Apache Mysql Php: mysql with php doesn't work breakerfall Linux - Networking 6 12-27-2003 08:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration