0

我有一个带有 3 个表的 MySQL 数据库:

**articles:**

id - articletitle - articleorganization - articleurl - articledate

**tags:**

id - articletag

articles_tags

**id:**
article_id - tag_id

在我的表单上,我有文本输入框来填写标题、组织、URL 和日期的信息,我有复选框来捕获标签信息。

这很好用,但是当我需要编辑数据库中的条目时问题就来了。有了这个,我有一个与输入表单类似的表单,但我无法让复选框工作。经过一些研究和 Stackoverflow 贡献者的大力帮助,我了解到我需要的是一种不同类型的数据库结构(这是我现在拥有的,我最初只有 1 个表,现在我有上面列出的 3 个)。

这导致我重新开发输入表单,因此现在我尝试将标题、组织、URL 和日期信息插入到文章表中,并将标签信息插入到标签表中,同时还在文章标签表中创建关系。

到目前为止,我在这个尝试中一直没有成功。到目前为止,这是我一直在使用的内容:

    <?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('','','','','');
     }
    ?>
4

2 回答 2

1

您在 SQL 查询中缺少关键字INTO - INSERT INTO mytable... 您还可以将最后 2 个表合并为一个 - “tag_id、article_id、article_tags”。还有一个建议 - 请阅读PDO是什么并摆脱 mysql_*。

于 2012-06-19T04:26:20.297 回答
1

下面是根据文章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');?>>
于 2012-06-19T06:05:38.227 回答