简体   繁体   English

建立MySQL数据库关系并更新现有的PHP代码

[英]Making a MySQL database relational and updating existing PHP code

I have a MySQL database called csftdb and in that database there exists 1 table: articles. 我有一个名为csftdb的MySQL数据库,在该数据库中有1个表:文章。

I also have a series of PHP forms that can add, edit, and delete entries to/from the table within the database. 我还有一系列PHP表单,可以在数据库中的表中添加,编辑和删除条目。

Now, however, I'm attempting to add search functionality and have found that the single-table method is insufficient. 但是,现在,我正在尝试添加搜索功能,并发现单表方法不足。

The table is laid out thus: 该表格如下:

id - articletitle - articleorganization - articledate - articleurl - articletags

One of the PHP forms is a search form and it works except when the tags come into play, for example: 其中一个PHP表单是一个搜索表单,它可以工作,除非标签发挥作用,例如:

Say there are 3 articles in the database: 假设数据库中有3篇文章:

01 - My first article - Article Corp. - 05/28/2010 - www.articlecorp.com/article1 - php, html
02 - My second article - Article Corp. - 08/11/2012 - www.articlecorp.com/article2 - asp, html
03 - My third article - Article Corp. - 12/22/2011 - www.articlecorp.com/article3 - c++, html

And then I have a search form with fields for Title, Organization, Date, URL, and Tag(s). 然后我有一个搜索表单,其中包含标题,组织,日期,URL和标记的字段。 I am able to search "My first article" in the Title field and only retrieve the 1st entry, but I can also search "article" in the Title field and it would retrieve all 3 entries. 我可以在标题字段中搜索“我的第一篇文章”,只检索第一个条目,但我也可以在标题字段中搜索“文章”,它将检索所有3个条目。 This is the desired behavior. 这是期望的行为。 Further, I can search "article" in the Title field and "Article Corp." 此外,我可以在标题字段和“文章公司”中搜索“文章”。 in the Organization field and it would only show entries that match that set (so, if there were other organizations that also had the word "article" in their article titles, those organizations would not show up in the results.) This is also desired behavior. 在组织字段中,它只显示与该集合匹配的条目(因此,如果其他组织在其文章标题中也有“文章”一词,那么这些组织将不会显示在结果中。)这也是期望的行为。

However, if I were to search "Article" in the title field, and then "php, asp, c++" in the Tag(s) field, my search would return no results. 但是,如果我在标题字段中搜索“文章”,然后在标记字段中搜索“php,asp,c ++”,则我的搜索将不返回任何结果。 This is not the desired behavior. 这不是理想的行为。 In this case, the "tags" are supposed to allow a more fine level of granularity with the search results. 在这种情况下,“标签”应该允许搜索结果具有更精细的粒度级别。

So I could conceivably have a thousand articles, all with the exact same Title etc. information but with different combinations of tags and I could search for the tags and retrieve all of the articles with just those tags applied. 所以我可以想象有一千篇文章,都有完全相同的标题等信息,但有不同的标签组合,我可以搜索标签,并检索所有文章只应用这些标签。

To that end, I have created 2 new tables in my database and altered the original one such that my tables are now: 为此,我在我的数据库中创建了2个新表,并更改了原始表,以便我的表现在是:

articles:
id | articletitle | articleorganization | articledate | articleurl


tags:
id | tags

and

articles_tags:
article_id | tag_id

I also have the following PHP form which is modified from the original form: 我还有以下PHP表单,它是从原始表单修改的:

        <?php
     function renderForm($articletitle, $articleorganization, $articledate, $articleurl, $articletags )
     {
     ?>
    . . .
      <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="articles.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 $articles.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="articles.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 $articles.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 $tags.articletags; ?>" size="50"/>
                </span></td>
              </tr>
            </table>
            <footer><input type="submit" name="submit" value="Add this Article"></footer>
            </form>
        </div>
        . . .
    </body>
    </html>
    <?php 
     }

     . . .
      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']));

     . . .

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


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

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

However I can't seem to get the information to store into my 2nd table and, thus, I can't get my relations to work. 但是我似乎无法将信息存储到我的第二张表中,因此,我无法让我的关系发挥作用。

I've been looking at this all day and can not figure it out. 我整天都在看这个,无法理解。

For each submitted tag you need to look if it exists or not, if it does not exists, you need to add it and then you store all the relations. 对于每个提交的标记,您需要查看它是否存在,如果它不存在,您需要添加它然后存储所有关系。

You should also compare with the original values if one or more tags have been removed and then check each removed tag if it was the only used variant of that tag so you can delete it from the tags and relations table. 如果已删除一个或多个标记,则还应与原始值进行比较,然后检查每个已删除标记(如果它是该标记的唯一使用变体),以便可以从标记和关系表中删除它。

You'll need to add a couple more INSERT queries to get this to work properly. 您需要添加几个INSERT查询才能使其正常工作。

if (mysql_query ("INSERT INTO `articles` SET
  `articletitle` = '$articletitle',
  `articleorganization` = '$articleorganization',
  `articledate` = '$articledate',
  `articleurl` = '$articleurl'")) {

  $article_id = mysql_insert_id();
}

$tags = split (' ', $_POST['articletags']);
foreach ($tags as $tag) {
  // check if tag exists in tag table and get id, if not, insert it and get mysql_insert_id()
  // then insert article_id with tag_ids into your articles_tags table
}

This is rough but its the idea. 这很粗糙,但它的想法。 Also, if I recall correctly "split" is depreciated so you'll want to find another option. 另外,如果我没记错,“拆分”会被折旧,所以你会想要找到另一个选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM