SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sql update page not working

    I have a page I'm testing directly out of the sitepoint build database driven Websites.

    It is the jokes.php page script from chapter 4. Here is my issue.

    When I got to insert a new joke and hit submit the textbox just reloads and nothing gets saved from the book. Here is the code I'm wondering what the problem could be.

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>The Internet Joke Database</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>

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

    <form action="<?php echo $_SERVER['jokes.php']; ?>" method="post">
    <label>Type your joke here:<br />
    <textarea name="joketext" rows="10" cols="40">
    </textarea></label><br />
    <input type="submit" value="SUBMIT" />
    </form>

    <?php else: // Default page display

      // Connect to the database server
      
    $dbcnx = @mysql_connect('localhost''clhdesi_christ''hitman1');
      if (!
    $dbcnx) {
        exit(
    '<p>Unable to connect to the ' .
            
    'database server at this time.</p>');
      }

      
    // Select the jokes database
      
    if (!@mysql_select_db('clhdesi_clhdesigns')) {
        exit(
    '<p>Unable to locate the joke ' .
            
    'database at this time.</p>');
      }

      
    // If a joke has been submitted,
      // add it to the database.
      
    if (isset($_POST['joketext'])) {
        
    $joketext $_POST['joketext'];
        
    $sql "INSERT INTO joke 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 joke');
      if (!
    $result) {
        exit(
    '<p>Error performing query: ' .
            
    mysql_error() . '</p>');
      }

      
    // 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="' $_SERVER['PHP_SELF'] .
          
    '?addjoke=1">Add a Joke!</a></p>';

    endif;
    ?>
    </body>
    </html>
    Your Websites Your Way!!
    www.clhdesigns.com

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess $_SERVER['jokes.php'] should be $_SERVER['SCRIPT_NAME'] (http://php.net/reserved.variables).

    I'm referring to this code:
    Code:
    <form action="<?php echo $_SERVER['jokes.php']; ?>" method="post">

  3. #3
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    i don't think this is the problem, but shouldn't $_SERVER['jokes.php'] be $_SERVER['PHP_SELF']?

  4. #4
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I forgot to post a link.

    www.clhdesigns.com/site2/jokes.php

    so you can see what I'm talking about.
    Your Websites Your Way!!
    www.clhdesigns.com

  5. #5
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help the variable should have been $_SERVER['PHP_SELF']; as explained in your responses thanks again.

    capples
    Your Websites Your Way!!
    www.clhdesigns.com

  6. #6
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another thing for safety and prevent data redundancy...
    try to show jokes on separate page.
    OR just try ur jokes page, add joke and then refresh the page it will add that joke again to ur db.

  7. #7
    SitePoint Wizard
    Join Date
    Jul 2003
    Location
    Kent
    Posts
    1,921
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If you hit the Refresh button, expecting to see any new jokes that have been added after yours, the same joke gets added a second time...
    (The data redundancy mentioned by mmarif4u)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •