Connected to mySQL through PhP but can't retrieve, add or delete records

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jessiK
    New Member
    • Nov 2008
    • 2

    Connected to mySQL through PhP but can't retrieve, add or delete records

    Pls help! I'm new to PHP and MySQL and I'm having a problem when working with my database (I am using IIS6). When using PHP I can connect to the database without any problems, I can even add new tables to the database but when I try to retrieve, add or delete records nothing happens. No error messages, nothing. I can run the queries successfully through phpMyAdmin and mySQL administrator. I don't think it's the php coding that I'm using because I have tried a number of scrips that I got off online tutorials and the results are always the same no matter which tutorial I use. What could I be missing?

    Thanks for your help!!
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    T'would help to see the code you use to run these queries, my friend.

    Markus.

    Comment

    • jessiK
      New Member
      • Nov 2008
      • 2

      #3
      Hi, sorry, this is one of the code snippets I've tried. It is part of a tutorial by Kevin Yank from his book "Building a Database-Driven Web Site Using PHP and MySQL".

      Code:
      <HTML>
      <BODY>
      <?php
        // If the user wants to add a joke
        if (isset($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", "root");
          if (!$dbcnx) {
            echo( "<P>Unable to connect to the " .
                  "database server at this time.</P>" );
            exit();
          }
      
          // Select the jokes database
          if (! @mysql_select_db("ijdb") ) {
            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" == $submitjoke) {
            $sql = "INSERT INTO Jokes 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 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>
      All that is displayed on the page is:

      Code:
      echo("<P> Here are all the jokes " .
               "in our database: </P>");
      and

      Code:
      echo("<P><A HREF='$PHP_SELF?addjoke=1'>" .
               "Add a Joke!</A></P>");
      If I click on the above link nothing happens.


      Yet if I run this piece of code it creates the new table witout a problem.
      Code:
      $sql = "CREATE TABLE Jokes ( " .
             "ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " .
             "JokeText TEXT, " .
             "JokeDate DATE NOT NULL " .
             ")";
      if ( mysql_query($sql) ) {
        echo("<P>Jokes table successfully created!</P>");
      } else {
        echo("<P>Error creating Jokes table: " .
             mysql_error() . "</P>");
      }
      Hope I've explained this correctly

      Thanks!
      Last edited by Markus; Nov 14 '08, 05:41 AM. Reason: added # tags

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Most likely this check is failing [CODE=php]if ("SUBMIT" == $submitjoke)[/CODE] because everything else depends on it. Verify that that part is returning true.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by r035198x
          Most likely this check is failing [CODE=php]if ("SUBMIT" == $submitjoke)[/CODE] because everything else depends on it. Verify that that part is returning true.
          Indeed. What is $submitjoke. I don't see it declared anywhere.

          Also, I was too tired/dying this morning to inform you:

          Please use code tags when posting your code. To do this, highlight the code, and then hit the '#' key at the top of the textarea. Read Posting Guidelines if you're still unsure about anything.

          Moderator.

          Comment

          Working...