2012-06-15 67 views
0

我有一個基本的PHP表單(幾個字段和2個複選框,我想稍後添加更多的複選框,但現在我只用2進行測試)在PHP中。字段和複選框保存到MySQL數據庫中,並且效果很好。複選框簽入狀態基於MySQL和PHP

我也有一個'編輯'窗體,它反映了原始輸入窗體,只是字段的值是根據行ID從MySQL數據庫加載的,這是非常標準的。

問題是,此刻,編輯窗體的最後一個字段只是另一個文本輸入字段。我想要做的是更改爲複選框,將根據原始表單的輸入來選中或取消選中。

我想有一個需要使用ischecked,但我不確定是否需要更改原始表單的邏輯,或者如果我需要更改數據庫本身(我希望不是!)。

數據庫正是如此佈局:

------------------------------------------------------------------------------------------- 
| ID (int) | ARTICLEAUTHOR (varchar) | ARTICLEORGANIZATION (varchar) | ARTICLETAGS (varchar)| 
------------------------------------------------------------------------------------------- 

而對於這兩種形式的代碼如下。

首先,新的報名表:

  // addnew.php 

      <?php 
      function renderForm($articletitle, $articleorganization, $articledate, $articleurl, $articletags) 
      . . . 
      </head> 

      <body> 
      . . . 
        <form id="form" name="form" action="" method="post"> 
        <h1>Create a new entry in the database</h1> 
        <table width="100%" border="0" cellpadding="6"> 
         <tr> 
         <td colspan="2"><legend>Article details</legend></td> 
         </tr> 
         <tr> 
         <td width="20%" align="right"><span class="field">Article Title:</span></td> 
         <td width="80%" align="left"><span class="field"> 
          <input name="articletitle" type="text" value="<?php echo $articletitle; ?>" size="50"/> 
         </span></td> 
         </tr> 
         <tr> 
         <td align="right"><span class="field">Article Author:</span></td> 
         <td align="left"><span class="field"> 
          <input name="articleorganization" type="text" value="<?php echo $articleorganization; ?>" size="50"/> 
         </span></td> 
         </tr> 
         <tr> 
         <td align="right"><span class="field">Article Date:</span></td> 
         <td align="left"><span class="field"> 
          <input name="articledate" type="text" value="<?php echo $articledate; ?>" size="50"/> 
         </span></td> 
         </tr> 
         <tr> 
         <td align="right"><span class="field">Article URL:</span></td> 
         <td align="left"><span class="field"> 
         <input name="articleurl" type="text" value="<?php echo $articleurl; ?>" size="50"/> 
         </span></td> 
         </tr> 
        </table> 
      . . . 
         <input type="checkbox" name="articletags[]" value="checkbox" id="articletags_0" /> 
         <input type="checkbox" name="articletags[]" value="checkbox 2" id="articletags_1" /> 

         </div> 
        </fieldset> 
        <footer><input type="submit" name="submit" value="Add this Article"></footer> 
      . . . 
        </form> 
       </div> 
       </div> 
      . . . 
      </body> 
      </html> 
      <?php 
      } 

      // connect to the database 
      include('settings.php'); 

      if(count($articletags) > 0) 
      { 
      $articletags_string = implode(",", $articletags); 
      } 
      // check if the form has been submitted. If it has, start to process the form and save it to the database 
      if($_SERVER['REQUEST_METHOD'] == 'POST') 
      { 
      // get form data, making sure it is valid 
      $articletitle = mysql_real_escape_string(htmlspecialchars($_POST['articletitle'])); 
      $articleorganization = mysql_real_escape_string(htmlspecialchars($_POST['articleorganization'])); 
      $articledate = mysql_real_escape_string(htmlspecialchars($_POST['articledate'])); 
      $articleurl = mysql_real_escape_string(htmlspecialchars($_POST['articleurl'])); 
      $articletags = implode(',', $_POST['articletags']); 

      . . . 

      mysql_query("INSERT articles SET articletitle='$articletitle', articleorganization='$articleorganization', articledate='$articledate', articleurl='$articleurl', articletags='$articletags' ") 
      or die(mysql_error()); 

      // once saved, redirect to success page 
      header("Location:addsuccess.php"); 
      } 
      } 
      else 
      // if the form hasn't been submitted, display the form 
      { 
      renderForm('','','',''); 
      } 
      ?> 

而現在的編輯形式:

  <?php 
      . . . 
      function renderForm($id, $articletitle, $articleorganization, $articledate, $articleurl, $articletags) 
      { 
      ?> 
      . . . 
       <div class="content"> 
       <div id="stylized" class="myform"> 
        <form id="form" name="form" action="" method="post"> 
        <input type="hidden" name="id" value="<?php echo $id; ?>"/> 
        <h1>Edit Details for &nbsp; &nbsp;<?php echo $articletitle; ?></h1> 
        <table width="100%" border="0" cellpadding="6"> 
         <tr align="center" valign="middle"> 
         <td colspan="2"><legend>Article details</legend></td> 
         </tr> 
         <tr> 
         <td width="26%" align="right"><span class="field">Article Title</span></td> 
         <td width="74%" align="left"><span class="field"> 
          <input name="articletitle" type="text" value="<?php echo $articletitle; ?>" size="50"/> 
         </span></td> 
         </tr> 
         <tr> 
         <td align="right"><span class="field">Article Author</span></td> 
         <td align="left"><span class="field"> 
          <input name="articleorganization" type="text" value="<?php echo $articleorganization; ?>" size="50"/> 
         </span></td> 
         </tr> 
         <tr> 
         <td align="right"><span class="field">Article Date</span></td> 
         <td align="left"><span class="field"> 
          <input name="articledate" type="text" value="<?php echo $articledate; ?>" size="50"/> 
         </span></td> 
         </tr> 
         <tr> 
         <td align="right"><span class="field">Article Url:</span></td> 
         <td align="left"><span class="field"> 
          <input name="articleurl" type="text" value="<?php echo $articleurl; ?>" size="50"/> 
         </span></td> 
         </tr> 
         <tr> 
         <td align="right"><span class="field">Article Tags:</span></td> 
         <td align="left"><span class="field"> 
          <input name="articletags" type="text" value="<?php echo $articletags; ?>" size="50"/> 
         </span></td> 
         </tr> 
         <tr align="center" valign="middle"> 
         <td colspan="2"><input type="submit" name="submit" value="Submit" /></td> 
         </tr> 
        </table> 
      . . . 
        </fieldset> 
        <footer></footer></form> 
       </div> 
       </div> 
       <div class="footer"> 
      . . . 
       <!-- end .footer --></div> 
      </body> 
      </html> 
      <?php 
      } 



      // connect to the database 
      include('settings.php'); 

      // check if the form has been submitted. If it has, process the form and save it to the database 
      if (isset($_POST['submit'])) 
      { 
      // confirm that the 'id' value is a valid integer before getting the form data 
      if (is_numeric($_POST['id'])) 
      { 
      // get form data, making sure it is valid 
      $id = $_POST['id']; 
      $articletitle = mysql_real_escape_string(htmlspecialchars($_POST['articletitle'])); 
      $articleorganization = mysql_real_escape_string(htmlspecialchars($_POST['articleorganization'])); 
      $articledate = mysql_real_escape_string(htmlspecialchars($_POST['articledate'])); 
      $articleurl = mysql_real_escape_string(htmlspecialchars($_POST['articleurl'])); 
      $articletags = mysql_real_escape_string(htmlspecialchars($_POST['articletags'])); 

      . . . 

      mysql_query("UPDATE articles SET articletitle='$articletitle', articleorganization='$articleorganization', articledate='$articledate', articleurl='$articleurl', articletags='$articletags' WHERE id=$id") 
      or die(mysql_error()); 

      . . . 
      { 

      // get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0) 
      if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) 
      { 
      // query db 
      $id = $_GET['id']; 
      $result = mysql_query("SELECT * FROM articles WHERE id=$id") 
      or die(mysql_error()); 
      $row = mysql_fetch_array($result); 

      // check that the 'id' matches up with a row in the databse 
      if($row) 
      { 

      // get data from db 
      $articletitle = $row['articletitle']; 
      $articleorganization = $row['articleorganization']; 
      $articledate = $row['articledate']; 
      $articleurl = $row['articleurl']; 
      $articletags = $row['articletags']; 

      // show form 
      renderForm($id, $articletitle, $articleorganization, $articledate, $articleurl, $articletags, ''); 
      } 
      . . . 
      ?> 
+0

通常,表單中的複選框將與數據庫中的布爾值相關聯。從你的代碼看起來你正在嘗試使用關聯複選框和articletags字段。是對的嗎? – mwotton

+0

是的,這完全正確@mwotton。 – Zrb0529

回答

1

你會好起來的有兩個新的表來跟蹤標籤。

你的文章表將失去標籤領域

標籤表結構如下:

Tags Table 
----- 
id (int) 
tagname varchar(64) 

ArticleHasTags交叉映射表具有以下結構

ArticleHasTags 
---- 
article_id (int) 
tag_id (int) 

然後你構建基於複選框標籤表條目。存儲文章時,您還將根據條目表單中的複選框將條目添加到ArticleHasTags表中。

當顯示編輯表單,你無論從文章表和ArticleHasTags表提取數據,無論是與多個連接或可能更容易做第二次選擇:

select * from ArticleHasTags where article_id = $id 

(其中的$ id是您正在編輯的文章的編號)

然後您再次從標籤數據庫中拉出所有行以顯示您的複選框。在顯示每個複選框時,您可以通過使用in_array()檢查標記ID與檢索到的ArticleHasTag結果來檢查文章何時具有該標記。

我可以進入更深入,但是我會判斷,這將是更有價值的你在長期使用這些指針和數字的細節出自己;)

這是非常程序,但沒有光顧,這看起來像你現有的代碼水平。受到鼓勵並不斷學習!你來到了正確的位置。

+0

這看起來很有趣。但我會認爲額外的複雜性會不好,但?如果有兩篇文章,都是由Sylvester Stallone編寫的,一篇冠軍爲One,另一篇冠軍爲Two,兩者都被標記爲「舉重」,但第一篇文章被標記爲「自由權重」,第二篇文章被標記爲「重量機器「,那麼就搜索能力而言,這種結構會不會增加複雜性? – Zrb0529