2012-06-19 3 views
0

나는 csftdb라는 MySQL 데이터베이스를 가지고 있으며 그 데이터베이스에는 하나의 테이블 : 기사가 있습니다.MySQL 데이터베이스를 관계형으로 만들고 기존 PHP 코드를 업데이트하십시오.

또한 데이터베이스의 테이블에 항목을 추가, 편집 및 삭제할 수있는 일련의 PHP 양식이 있습니다.

그러나 지금은 검색 기능을 추가하려고 시도하고 있으며 단일 테이블 방법이 충분하지 않음을 발견했습니다. PHP는 형태의

id - articletitle - articleorganization - articledate - articleurl - articletags 

하나는 검색 폼이며 태그가 예를 들어, 활동하기 시작하는 경우를 제외하고 작동합니다 :

표는 따라서 배치되어

이 말해 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 개의 항목을 모두 검색 할 수 있습니다. 이것이 바람직한 행동입니다. 또한, Title 필드에서 "article"을 검색 할 수 있고 "Article Corp." 조직 필드에는 해당 세트와 일치하는 항목 만 표시됩니다 (즉, 기사 제목에 "article"이라는 단어가있는 다른 조직이있는 경우 해당 조직은 결과에 표시되지 않습니다). 행동.

그러나 제목 필드에서 "Article"을 검색 한 다음 태그 필드에 "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 
} 

이것은 거친 내용이지만 그 아이디어입니다. 또한, 정확하게 기억한다면 "split"은 가치가 떨어지고 다른 옵션을 찾고 싶을 것입니다.

+0

'폭발 '할 것입니다. – hakre

1

제출 된 각 태그의 존재 여부를 확인해야합니다. 존재하지 않는 경우 추가하고 모든 관계를 저장해야합니다.

하나 이상의 태그가 제거 된 경우 원래 값과 비교 한 다음 제거 된 각 태그가 해당 태그의 유일한 변형 인 경우 태그 및 관계 테이블에서 해당 태그를 삭제할 수 있는지 확인해야합니다.

+0

체크 박스 그룹을 사용할 생각이었습니다. 그렇게하면 태그 사용에 일관성을 적용 할 수 있습니다. 그 방법으로 나는 태그가 존재하는지 여부를 이미 알고있을 것이다. – Zrb0529

관련 문제