我有一个名为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('','','','','');
}
?>
但是我似乎无法获得信息离子存储到我的第二张桌子,因此,我不能让我的关系工作。
我一直在寻找这一切的一天,并且不能弄明白。
'爆炸'将做到这一点。 – hakre