SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 1999
    Location
    Singapore
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello, I'm encountering an error with newjoke.php at http://www.adolescentadulthood.com/phptest/newjoke.php

    It gives me an error, and i dont know how to correct it. Can someone please take a look and help?
    Adolescent Adulthood - Learn All ABout Flirting, Dating And Dumping.

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    theprof,

    Any chance you can make the code you're using available as a .TXT file on your sever? Blank out your database username/password of course, though. I can't diagnose the problem without seeing the code.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 1999
    Location
    Singapore
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure. Its the same newjoke.php provided in your tutorial.

    Code:
    <!-- newjoke.php -->
    <HTML>
    <HEAD>
    <TITLE> Add New Joke </TITLE>
    </HEAD>
    <BODY>
    <?php
    
    if ($submit): // A new joke has been entered
                  // using the form.
    
      if ($aid == "") {
        echo("<P>You must choose an author " .
             "for this joke. Click 'Back' " .
             "and try again.</P>");
        exit();
      }
    
    require ("dbcnx.php");
    
      $sql = "INSERT INTO Jokes SET " .
             "JokeText='$joketext', " .
             "AID='$aid'";
      if (mysql_query($sql)) {
        echo("<P>New joke added</P>");
      } else {
        echo("<P>Error adding new joke: " .
             mysql_error() . "</P>");
      }
    
      $jid = mysql_insert_id();
    
      $cats = mysql_query("SELECT ID, Name FROM Categories");
    
      while ($cat = mysql_fetch_array($cats)) {
        $cid = $cat["ID"];
        $cname = $cat["Name"];
        $var = "cat$cid"; // The name of the variable
        if ($$var) { // The checkbox is checked
          $sql = "INSERT IGNORE INTO JokeLookup " .
                 "SET JID=$jid, CID=$cid";
          $ok = mysql_query($sql);
          if ($ok) {
            echo("<P>Joke added to category: $cname</P>");
          } else {
            echo("<P>Error inserting joke into category $cname:" .
                 mysql_error() . "</P>");
          }
        } // end of if ($$var)
      } // end of while loop
    ?>
    
    <P><A HREF="<?php echo($PHP_SELF); ?>">Add another Joke</A></P>
    <P><A HREF="jokes.php">Return to Joke Search</A></P>
    
    <?php
      else: // Allow the user to enter a new category
    
      $dbcnx = @mysql_connect("localhost", "root", "mypasswd");
      mysql_select_db("jokes");
    
      $authors = mysql_query("SELECT ID, Name FROM Authors");
      $cats    = mysql_query("SELECT ID, Name FROM Categories");
    ?>
    
    <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
    <P>Enter the new joke:<BR>
    <TEXTAREA NAME="joketext" ROWS=15 COLS=45 WRAP>
    </TEXTAREA>
    <P>Author:
    <SELECT NAME="aid" SIZE=1>
      <OPTION SELECTED VALUE="">Select One
      <OPTION VALUE="">---------
    <?php
      while ($author = mysql_fetch_array($authors)) {
        $aid = $author["ID"];
        $aname = $author["Name"];
        echo("<OPTION VALUE='$aid'>$aname\n"); 
      }
    ?>
    </SELECT></P>
    <P>Place in categories:<BR>
    <?php
      while ($cat = mysql_fetch_array($cats)) {
        $cid = $cat["ID"];
        $cname = $cat["Name"];
        echo("<INPUT TYPE=CHECKBOX NAME='cat$cid'>$cname<BR>\n"); 
      }
    ?>
    </P>
    <P><INPUT TYPE=SUBMIT NAME="submit" VALUE="SUBMIT"></P>
    </FORM>
    <?php endif; ?>
    </BODY>
    </HTML>
    Adolescent Adulthood - Learn All ABout Flirting, Dating And Dumping.

  4. #4
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    As I mentioned in my article, this sample code omits all error checking in the interest of brevity. As a result, it does not tell you when one of your MySQL queries fails.

    You should modify your code so that, when an error does occur, it is deteced and displayed. For example, consider the following line:

    Code:
    $cats = mysql_query("SELECT ID, Name FROM Categories");
    Instead of just assuming this will work, check it:

    Code:
    $cats = mysql_query("SELECT ID, Name FROM Categories");
    if (!$cats) {
      echo("MySQL Error: " . mysql_error());
      exit;
    }
    Now your Web page should display the actual error message that is being produced by your database. If you need any further help fixing the problem, feel free to ask!
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 1999
    Location
    Singapore
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found the source of my error. You wont believe this, but i left my mysql password as mypasswd.
    Adolescent Adulthood - Learn All ABout Flirting, Dating And Dumping.

  6. #6
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The simplest errors have all of us pulling our hair out at times.
    Wayne Luke
    ------------


  7. #7
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Good error-checking code helps us spot silly mistakes like these. :-)
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  8. #8
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LOL - that's happened to me before to. Boy did I look foolish after yelling at my computer for being so stupid


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
  •