2012-06-19 46 views
0

我有一个名为csftdb并在该数据库中存在1台MySQL数据库:文章。使MySQL数据库关系和更新现有的PHP代码

我也有一系列的PHP形式,可以添加,编辑,并从数据库中的表中删除条目/。

但是现在,我尝试添加搜索功能,并已发现,单表的方法是不够的。

该表是由此埋下了:

id - articletitle - articleorganization - articledate - articleurl - articletags 

其中一个PHP的形式是搜索的形式和它的作品除了当标签开始发挥作用,例如:

说,有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 

然后我有一个标题,组织,日期,URL和标记字段的搜索表单。我可以在标题字段中搜索“我的第一篇文章”,并只检索第一个条目,但我也可以在标题字段中搜索“文章”,它将检索所有3个条目。这是所需的行为。此外,我可以在标题字段和“Article Corp.”中搜索“文章”在组织领域,它只会显示与该组相匹配的条目(因此,如果有其他组织在其文章标题中也有“文章”一词,那么这些组织就不会出现在结果中)。这也是期望的行为。

但是,如果我是在搜索标签(一个或多个)字段的标题字段“条”,然后是“PHP,ASP,C++”,我的搜索将返回任何结果。这不是理想的行为。在这种情况下,“标签”应该使搜索结果具有更精细的粒度级别。

所以,我可以想见,有千余条,全部采用完全相同的名称等信息,但与标签不同的组合,我可以搜索广告代码,并不仅仅是那些应用的代码检索所有的文章。

为此,我在数据库中创建了2个新表,并改变了原来的一个,使得我的表,现在是:

articles: 
id | articletitle | articleorganization | articledate | articleurl 


tags: 
id | tags 

articles_tags: 
article_id | tag_id 

我也有下面的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('','','','',''); 
    } 
    ?> 

但是我似乎无法获得信息离子存储到我的第二张桌子,因此,我不能让我的关系工作。

我一直在寻找这一切的一天,并且不能弄明白。

回答

1

你需要添加一对夫妇更多的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 
} 

这很粗糙,但它的想法。另外,如果我记得正确的“拆分”是折旧,所以你会想找到另一种选择。

+0

'爆炸'将做到这一点。 – hakre

1

对于你需要看如果它存在与否,如果不存在,它每个提交的标签,您需要添加它,然后你存储所有的关系。

你也应该与原始值进行比较,如果一个或多个标签已被删除,然后检查每个删除标记,如果它是标签的唯一使用的变种,因此您可以从标签和关系表中删除它。

+0

我在考虑使用复选框组。这样我就可以强化标签使用的一致性。用这种方法,我已经知道标签是否存在。 – Zrb0529