我有一个名为csftdb的MySQL数据库,在该数据库中有一个表:文章。
我还有一系列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个条目。这是所需的行为。此外,我可以在标题字段中搜索“文章”和“文章公司”。在“组织”字段中,它将只显示与该集合匹配的条目(因此,如果有其他组织的文章标题中也包含“文章”一词,则这些组织不会显示在结果中。)这也是所需的行为。
但是,如果我在标题字段中搜索“文章”,然后在标签字段中搜索"php,asp,c++“,我的搜索将不会返回任何结果。这不是我们想要的行为。在这种情况下,“标签”应该允许搜索结果具有更精细的粒度。
所以可以想象,我可以有一千篇文章,所有文章都有完全相同的标题等信息,但具有不同的标签组合,我可以搜索标签并检索所有应用了这些标签的文章。
为此,我在我的数据库中创建了两个新表,并修改了原来的表,使我的表现在为:
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('','','','','');
}
?>然而,我似乎无法将信息存储到我的第二个表中,因此,我无法让我的关系工作。
我已经看了一整天了,还是想不明白。
发布于 2012-06-19 09:37:13
您需要添加更多的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“是折旧的,所以你会想要找到另一个选项。
发布于 2012-06-19 09:31:27
对于每个提交的标签,你需要检查它是否存在,如果它不存在,你需要添加它,然后存储所有的关系。
如果已删除一个或多个标记,则还应与原始值进行比较,然后检查每个已删除的标记是否为该标记的唯一使用变体,以便可以将其从标记和关系表中删除。
https://stackoverflow.com/questions/11093230
复制相似问题