Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Newbie
 
Join Date: Nov 2008
Posts: 2
#1: Nov 13 '08
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's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#2: Nov 13 '08

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


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

Markus.
Newbie
 
Join Date: Nov 2008
Posts: 2
#3: Nov 14 '08

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


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".

Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2. <BODY>
  3. <?php
  4.   // If the user wants to add a joke
  5.   if (isset($addjoke)):
  6. ?>
  7.  
  8. <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
  9. <P>Type your joke here:<BR>
  10. <TEXTAREA NAME="joketext" ROWS=10 COLS=40 WRAP>
  11. </TEXTAREA><BR>
  12. <INPUT TYPE=SUBMIT NAME="submitjoke" VALUE="SUBMIT">
  13. </FORM>
  14.  
  15. <?php
  16.   else:
  17.  
  18.     // Connect to the database server
  19.     $dbcnx = @mysql_connect("localhost",
  20.              "root", "root");
  21.     if (!$dbcnx) {
  22.       echo( "<P>Unable to connect to the " .
  23.             "database server at this time.</P>" );
  24.       exit();
  25.     }
  26.  
  27.     // Select the jokes database
  28.     if (! @mysql_select_db("ijdb") ) {
  29.       echo( "<P>Unable to locate the joke " .
  30.             "database at this time.</P>" );
  31.       exit();
  32.     }
  33.  
  34.     // If a joke has been submitted,
  35.     // add it to the database.
  36.     if ("SUBMIT" == $submitjoke) {
  37.       $sql = "INSERT INTO Jokes SET " .
  38.              "JokeText='$joketext', " .
  39.              "JokeDate=CURDATE()";
  40.       if (mysql_query($sql)) {
  41.         echo("<P>Your joke has been added.</P>");
  42.       } else {
  43.         echo("<P>Error adding submitted joke: " .
  44.              mysql_error() . "</P>");
  45.       }
  46.     }
  47.  
  48.     echo("<P> Here are all the jokes " .
  49.          "in our database: </P>");
  50.  
  51.     // Request the text of all the jokes
  52.     $result = mysql_query(
  53.               "SELECT JokeText FROM Jokes");
  54.     if (!$result) {
  55.       echo("<P>Error performing query: " .
  56.            mysql_error() . "</P>");
  57.       exit();
  58.     }
  59.  
  60.     // Display the text of each joke in a paragraph
  61.     while ( $row = mysql_fetch_array($result) ) {
  62.       echo("<P>" . $row["JokeText"] . "</P>");
  63.     }
  64.  
  65.     // When clicked, this link will load this page
  66.     // with the joke submission form displayed.
  67.     echo("<P><A HREF='$PHP_SELF?addjoke=1'>" .
  68.          "Add a Joke!</A></P>");
  69.  
  70.   endif;
  71.  
  72. ?>
  73. </BODY>
  74. </HTML>
All that is displayed on the page is:

Expand|Select|Wrap|Line Numbers
  1. echo("<P> Here are all the jokes " .
  2.          "in our database: </P>");
and

Expand|Select|Wrap|Line Numbers
  1. echo("<P><A HREF='$PHP_SELF?addjoke=1'>" .
  2.          "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.
Expand|Select|Wrap|Line Numbers
  1. $sql = "CREATE TABLE Jokes ( " .
  2.        "ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " .
  3.        "JokeText TEXT, " .
  4.        "JokeDate DATE NOT NULL " .
  5.        ")";
  6. if ( mysql_query($sql) ) {
  7.   echo("<P>Jokes table successfully created!</P>");
  8. } else {
  9.   echo("<P>Error creating Jokes table: " .
  10.        mysql_error() . "</P>");
  11. }
Hope I've explained this correctly

Thanks!
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Nov 14 '08

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


Most likely this check is failing
Expand|Select|Wrap|Line Numbers
  1. if ("SUBMIT" == $submitjoke)
because everything else depends on it. Verify that that part is returning true.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#5: Nov 14 '08

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


Quote:

Originally Posted by r035198x

Most likely this check is failing

Expand|Select|Wrap|Line Numbers
  1. if ("SUBMIT" == $submitjoke)
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.
Reply