用PHP和MySQL構建一個資料庫驅動的網站(7)

來源:互聯網
上載者:User
mysql|資料|資料庫 現在我們已經有了允許使用者輸入一個笑話並將其加入到我們的資料庫中的程式碼。現在剩下的就是將其加入到我們已做好的笑話顯示頁面。因為絕大多數的使用者只會想要看看笑話,所以我們不想對我們的頁面做大的更改,除非使用者表示想要添加一個新的笑話。因為這個原因,我們的應用程式應該是一個多功能的頁面。下面是程式的代碼:


<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", "mypasswd");
    if (!$dbcnx) {
      echo( "<P>Unable to connect to the " .
            "database server at this time.</P>" );
      exit();
    }

    // Select the jokes database
    if (! @mysql_select_db("jokes") ) {
      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>



  現在我們有了一個單獨的檔案,這個檔案包含不太多的PHP代碼,通過這個檔案,我們可以顯示我們的MySQL資料庫中的笑話並能向我們的MySQL資料庫中添加笑話。

一個挑戰

  作為家庭作業,你可以看看你是不是能解決這麼一個問題:在頁面上顯示的每一個笑話後面放置一個叫“Delete this Joke”的超串連,當單擊這個串連時,會從資料庫中刪除這個笑話並顯示更改過以後的笑話列表。下面是對你的一些提示:

  你可以還在一個多功能頁面完成全部的功能。

  你需要使用SQL的DELETE命令,這個命令我們曾在第二章中學習過。

  這是一個關鍵的問題。要刪除一個指定的資料庫,你需要能夠唯一地標識它。Jokes表中的ID可以完成這個功能。你必須將要被刪除的笑話的ID傳遞到刪除笑話的請求中。將這個值放到“Delete this Joke”串連的查詢字串中是比較合適的。

  如果你覺得你已經有了答案,或者你只想知道解決方案,那就去看看下一頁。祝你好運!

結語

  在這一章中,我們學習了一些新的用來實現與MySQL資料庫服務介面的PHP函數。使用這些函數,我們建立了我們的第一個資料庫驅動的網站,我們的這個網站可以線上地發布我們資料庫中笑話並允許訪問者向其中添加他們自己的笑話。

  在第五章中,我們會回到MySQL命令列去學習如何使用關係型資料庫的原理以及其他一些更進階的SQL查詢去描述更為複雜的資訊,並給予訪問者對他們自己添加的笑話以特別的許可權!

挑戰的解決

  這是我們上面提出的“家庭作業”的解決方案。要在每一個笑話後面添加一個“Delete this Joke”串連,必須作下面的改動:

  之前,我們曾經在我們的頁面的底端的“Add a Joke!”串連中傳遞過一個$addjoke變數,通過這個變數來通知我們的指令碼不再顯示通常的笑話列表,而是顯示一個錄入笑話的表單。與此相類似,我們在我們的“Delete this Joke”串連中傳遞一個$deletejoke變數來表示我們想要刪除一個笑話。

  在獲得每一個笑話的JokeText列的同時,我們還獲得了ID列的值,所以我們獲得了與資料庫中每一個笑話關聯的ID。

  我們將要刪除的笑話的ID值賦予$deletejoke變數。這是通過將從資料庫中獲得的ID值插入到每一個笑話的“Delete this Joke”串連中來實現的。

  使用了一個if 語句,如果在載入這一頁時,我們的$deletejoke被賦予了一個值(使用isset函數),我們會在一個SQLDELETE語句中使用這個值(將被刪除的笑話的ID)來刪除指定的笑話。

  這兒是全部的原始碼:


<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", "mypasswd");
    if (!$dbcnx) {
      echo( "<P>Unable to connect to the " .
            "database server at this time.</P>" );
      exit();
    }

    // Select the jokes database
    if (! @mysql_select_db("jokes") ) {
      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>");
      }
    }

    // If a joke has been deleted,
    // remove it from the database.
    if (isset($deletejoke)) {
      $sql = "DELETE FROM Jokes " .
             "WHERE ID=$deletejoke";
      if (mysql_query($sql)) {
        echo("<P>The joke has been deleted.</P>");
      } else {
        echo("<P>Error deleting joke: " .
             mysql_error() . "</P>");
      }
    }
      echo("<P> Here are all the jokes " .
         "in our database: </P>");
      // Request the ID and text of all the jokes
    $result = mysql_query(
                "SELECT ID, JokeText FROM Jokes");
    if (!$result) {
      echo("<P>Error performing query: " .
           mysql_error() . "</P>");
      exit();
    }
    // Display the text of each joke in a paragraph
    // with a "Delete this Joke" link next to each.
   while ( $row = mysql_fetch_array($result) ) {
      $jokeid = $row["ID"];
      $joketext = $row["JokeText"];
      echo("<P>$joketext " .
           "<A HREF='$PHP_SELF?deletejoke=$jokeid'>" .
           "Delete this Joke</A></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>




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.