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?
| SitePoint Sponsor |
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.
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
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.
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:
Instead of just assuming this will work, check it:Code:$cats = mysql_query("SELECT ID, Name FROM Categories");
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!Code:$cats = mysql_query("SELECT ID, Name FROM Categories"); if (!$cats) { echo("MySQL Error: " . mysql_error()); exit; }
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
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.
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





LOL - that's happened to me before to. Boy did I look foolish after yelling at my computer for being so stupid
Bookmarks