0

我在 PHP 中有一个基本的 PHP 表单(几个字段和 2 个复选框,我想稍后添加更多复选框,但现在我只使用 2 个进行测试)。字段和复选框保存到 MySQL 数据库,效果很好。

我还有一个“编辑”表单,它反映了原始输入表单,除了字段的值是根据行 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, '');
             }
            . . .
            ?>
4

1 回答 1

1

最好有两个新表来跟踪标签。

您的文章表将丢失标签字段

具有以下结构的标签表:

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

具有以下结构的 ArticleHasTags 交叉映射表

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

然后根据标签表条目构建复选框。存储文章时,您还将根据条目表单中的复选框将条目添加到 ArticleHasTags 表中。

显示编辑表单时,您从 Article 表和 ArticleHasTags 表中提取数据,或者使用多个连接,或者进行第二次选择可能更容易:

select * from ArticleHasTags where article_id = $id 

(其中 $id 是您正在编辑的文章的 ID)

然后,您再次从标签数据库中提取所有行以显示您的复选框。当您显示每个复选框时,您可以通过使用 in_array() 针对检索到的 ArticleHasTag 结果检查标签 ID 来检查文章何时具有该标签。

我可以更深入,但是我认为从长远来看,使用这些指针并自己弄清楚细节对你来说会更有价值;)

这一切都是非常程序化的,但没有光顾,这看起来就像您对现有代码的水平。受到鼓励并继续学习!你来对地方了。

于 2012-06-15T01:47:23.790 回答