[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

Learning PHP - Problem in my code

I'm learning PHP and I'm on day 2 of training. For one of my exercises, I'm trying to create a multipurpose page that displays a list of jokes in the database and allows me to Add more jokes to it from a form.

But when I enter a joke into the form nothing happens and after checking the database the joke wasn't added either.

You can check the code below. I've commented it to make it quicker to scan.

Also, does anyone have suggestions for how to find these problems a little easier? I seem to spend 10 minutes writing the code and an hour trying to find the ; I forgot to add. I don't have an eye for finding these things yet.
0
QuesterIM
Asked:
QuesterIM
  • 8
  • 3
  • 3
  • +1
2 Solutions
 
nplibCommented:
you didn't add any code.
0
 
gamebitsCommented:
You can add at the top of the page right after the php opening tag

error_reporting(E_ALL);

this will tell you where the script stop running
0
 
QuesterIMAuthor Commented:
<?php if (isset($_GET['addjoke'])): //User wants to add a joke
?>

    <form action="<?php echo $SERVER['PHP_SELF']; ?>" 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: // Deault page display

      //connect to the database server
      $dbcnx = @mysql_connect('localhost', 'root', '');
      if (!dbcnx) {
            exit('<p>Unable to connect to the database server at this time.</p>');
      }
      
      // Select the jokes database
      if (!mysql_select_db('ijdb')) {
            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=CUREDATE()";
            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;

?>
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
nplibCommented:
Do you get any one of your notifications? some telling you that it failed or succeeded?
0
 
QuesterIMAuthor Commented:
Nope it doesn't tell me nothing. Doesn't give me one of those weird errors and it doesn't give me one of my custom made errors either.

It just refreshes the page and the form stays up there and nothing happens.
0
 
QuesterIMAuthor Commented:
Ok I put in gamebits code and got this.

Notice: Use of undefined constant dbcnx - assumed 'dbcnx' in C:\xampp\htdocs\practice\jokelist.php on line 26

Here are all the jokes in our database:

Notice: Use of undefined constant result - assumed 'result' in C:\xampp\htdocs\practice\jokelist.php on line 53

Why did the chicken cross the road? To get to the other side!

Add a joke!
0
 
Roger BaklundCommented:
Remove the @ prefix for all your function calls. This prevents error messages.
0
 
gamebitsCommented:
This statement is wrong

 $sql = "INSERT INTO joke SET
                  joketext='$joketext',
                  jokedate=CUREDATE()";

SET is use with an update of an existing row  

UPDATE tablename
        SET fieldname = 'newvalue'
     WHERE fieldname = 'somedata'

to do an insert you need

INSERT INTO tablename (filedname, fieldname)VALUES('value1', 'value2')
0
 
Roger BaklundCommented:
This:

 if (!dbcnx) {

... should be:

 if (!$dbcnx) {

and this:

 if (!result) {

...should be:

 if (!$result) {

...and CUREDATE() should be CURDATE().
0
 
Roger BaklundCommented:
gamebits, it is perfectly legal syntax to use SET with an INSERT statement:

http://dev.mysql.com/doc/refman/5.1/en/insert.html

I even think it is preferrable, it is easy to mess up the fields with the VALUES () syntax.
0
 
QuesterIMAuthor Commented:
Ok I've removed all the @'s.

And I fixed the $dbcnx and !$result and the CURDATE(). *BLUSH*

 Now when I enter a joke into the form I get :

Object not found!

The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

If you think this is a server error, please contact the webmaster.
Error 404
localhost
02/20/09 23:13:26
Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i mod_autoindex_color PHP/5.2.8
0
 
gamebitsCommented:
@cxr thank you, I didn't know that.
0
 
QuesterIMAuthor Commented:
Also I notice the address bar says:

http://localhost/practice/<br /><b>Notice</b>::  Undefined variable: SERVER in <b>C:\xampp\htdocs\practice\jokelist.php</b> on line <b>15</b><br />
0
 
QuesterIMAuthor Commented:
Nevermind I know the problem there. I put $SERVER rather than $_SERVER
0
 
QuesterIMAuthor Commented:
It works now :P

Thanks to crx for seeing all my noob mistakes :P

And Thanks gamebits for the  The error_reporting(E_ALL); code. It's going to help a lot.
0
 
QuesterIMAuthor Commented:
Thanks again :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now