私はcsftdbと呼ばれるMySQLデータベースを持っており、そのデータベースには1つのテーブルがあります:articles。
また、データベース内のテーブルにエントリを追加、編集、およびテーブルからエントリを削除できる一連のPHPフォームもあります。
しかし、今、私は検索機能を追加しようとしていますが、単一テーブルの方法では不十分であることがわかりました。
テーブルは次のようにレイアウトされています。
id - articletitle - articleorganization - articledate - articleurl - articletags
PHPフォームの1つは検索フォームであり、タグが機能する場合を除いて機能します。次に例を示します。
データベースに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つのエントリすべてが取得されます。これは望ましい動作です。さらに、タイトル欄の「記事」と「ArticleCorp。」を検索できます。[組織]フィールドに、そのセットに一致するエントリのみが表示されます(したがって、記事のタイトルに「記事」という単語が含まれている他の組織がある場合、それらの組織は結果に表示されません)。これも望ましいです。行動。
ただし、タイトルフィールドで「Article」を検索し、次にTag(s)フィールドで「php、asp、c ++」を検索した場合、検索結果は返されません。これは望ましい動作ではありません。この場合、「タグ」は、検索結果でより細かいレベルの粒度を可能にすることになっています。
したがって、タイトルなどの情報がまったく同じで、タグの組み合わせが異なる1000の記事を作成し、タグを検索して、それらのタグだけを適用したすべての記事を取得できると考えられます。
そのために、データベースに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('','','','','');
}
?>
ただし、2番目のテーブルに格納する情報を取得できないため、関係を機能させることができません。
私はこれを一日中見ていましたが、理解できません。