簡體   English   中英

使用PHP表單將復選框信息傳遞給MySQL數據庫

[英]Using a PHP form to communicate checkbox information to a MySQL database

我有一個包含3個表的MySQL數據庫:

**articles:**

id - articletitle - articleorganization - articleurl - articledate

**tags:**

id - articletag

articles_tags

**id:**
article_id - tag_id

在我的表單上,我有文本輸入框來填寫標題,組織,URL和日期的信息,我有復選框來捕獲標簽信息。

這非常有效,但是當我需要編輯數據庫中的條目時,問題就出現了。 有了這個,我有一個類似於報名表的形式,但我不能讓復選框工作。 經過一些研究和Stackoverflow貢獻者的大力幫助,我了解到我需要的是一種不同類型的數據庫結構(這就是我現在擁有的,我最初只有一個表,現在我已經列出了上面列出的3個)。

這導致我重新開發輸入表單,所以現在我嘗試將標題,組織,URL和日期信息插入到articles表中,並將Tag信息插入到tags表中,同時還在articles_tags表中創建關系。

到目前為止,我在這次嘗試中都沒有成功。 這是我到目前為止一直在使用的內容:

    <?php
     function renderForm($articletitle, $articleorganization, $articledate, $articleurl, $articletags )
     {
     ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    . . .
    </head>

    <body>

    <div class="container">
      <div class="header">
      . . .
      </div>
        <div class="sidebar1">
        . . .
        </div>
      <div class="content">
            <div id="stylized" class="myform">
          <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">Access Date:</span></td>
                <td align="left"><span class="field">
                  <input name="articledate" type="text" value="MM/DD/YYYY" 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 type="checkbox" name="articletags[]" value="geology" id="articletags_0" />
                        <input type="checkbox" name="articletags[]" value="astronomy" id="articletags_1" />
                </span></td>
              </tr>
            </table>
            <footer><input type="submit" name="submit" value="Add this Article"></footer>
            </form>
        </div>
      <div class="footer">
        . . .
        </div>
    </body>
    </html>
    <?php 
     }

     include('settings.php');

     if(count($articletags) > 0)
    {
     $articletags_string = implode(",", $articletags);
    }

     if($_SERVER['REQUEST_METHOD'] == 'POST')
    { 

     $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' ")

    mysql_query2("INSERT tags SET articletags='$articletags' ")


     header("Location:addsuccess.php"); 
     }
     }
     else

     {
     renderForm('','','','','');
     }
    ?>

您在SQL查詢中缺少關鍵字INTO - INSERT INTO mytable ...您還可以將最后兩個表合並為一個 - “tag_id,article_id,article_tags”。 還有一條建議 - 請閱讀PDO是什么並擺脫mysql_ *。

下面是根據文章ID從db中檢索標記值。

<?php

    $checked=array();
    $sql = $dbh->prepare('
            SELECT t.articletag from tags t INNER JOIN article_tag a 
            ON t.id=a.tag_id 
            WHERE article_id=:article_id
            ');
    $sql->bindParam(':article_id', $articleid);        
    $sql->execute();
    $result = $sql->fetchAll(); <----- fetch All the data

    $vals=explode(',',$result['articletag']);


    foreach ($vals as $val)
    {
        if ($val !='')
            $checked[$val]='checked';
    }

    function set_checked($value)
    {            
        if (isset($checked[$value]))
            return 'checked';
        else
            return '';
    }

    ?>

HTML代碼,用於顯示是否根據檢索的值選中或取消選中復選框。

<input type="checkbox" name="articletags[]" value="geology" <? echo set_checked('geology');?>>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM