国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

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

2024-07-24 12:56:16
字體:
來源:轉載
供稿:網友
現在我們已經有了允許用戶輸入一個笑話并將其加入到我們的數據庫中的程序代碼。現在剩下的就是將其加入到我們已做好的笑話顯示頁面。因為絕大多數的用戶只會想要看看笑話,所以我們不想對我們的頁面做大的更改,除非用戶表示想要添加一個新的笑話。因為這個原因,我們的應用程序應該是一個多功能的頁面。下面是程序的代碼:


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


中國最大的web開發資源網站及技術社區,
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 四平市| 泾阳县| 勃利县| 赣榆县| 来宾市| 康马县| 普兰店市| 晋州市| 盈江县| 新宾| 寻甸| 木里| 陇川县| 池州市| 临夏县| 若羌县| 茂名市| 阿尔山市| 阳谷县| 宜君县| 沭阳县| 错那县| 玉树县| 巩义市| 荣昌县| 上虞市| 上饶县| 米易县| 高阳县| 海门市| 庆安县| 广德县| 威信县| 青神县| 玉环县| 蒙城县| 潍坊市| 汤阴县| 安陆市| 信宜市| 闵行区|