我有以下 PHP 表单用于在 MySQL 数据库中创建条目:
<?php
function renderForm($articletitle, $articleorganization, $articledate, $articleurl, $articletags )
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
. . .
</head>
<body>
<div class="container">
<div class="header">
. . .
</div>
<div class="sidebar1">
. . .
</div>
<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="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 $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="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 $articleurl; ?>" size="50"/>
</span></td>
</tr>
<tr>
<td align="right"><span class="field">Article Tags:</span></td>
<td align="left"><span class="field">
<input type="checkbox" name="articletags[]" value="geology" id="articletags_0" />
<input type="checkbox" name="articletags[]" value="astronomy" id="articletags_1" />
</span></td>
</tr>
</table>
<footer><input type="submit" name="submit" value="Add this Article"></footer>
</form>
</div>
<div class="footer">
. . .
</div>
</body>
</html>
<?php
}
include('settings.php');
if (count($articletags) > 0)
{
$articletags_string = implode(",", $articletags);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$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']));
$articletags = implode(',', $_POST['articletags']);
if ($articletitle == '' || $articleorganization == '')
{
$error = 'ERROR: Please fill in all required fields!';
renderForm($articletitle, $articleorganization);
}
else
{
mysql_query("INSERT INTO articles SET articletitle='$articletitle', articleorganization='$articleorganization', articledate='$articledate', articleurl='$articleurl' ");
mysql_query("INSERT INTO articles_tags SET articletags='$articletags' ")
or die(mysql_error());
header("Location:addsuccess.php");
}
}
else
{
renderForm('','','','','');
}
?>
目前,表单将向数据库中的两个表提交正确的信息,articles
和articles_tags
,但我不知道如何建立关系。
最终结果是单独表单上的复选框将是可编辑的(也就是说,如果我在输入页面上选中一个框,然后转到编辑页面,则该框已经被选中,然后可以取消选中,并且条目可以更新)。
不过,现在,我正试图使该关系出现在第二个表中。表的布局是:
articles
:
编号 | 文章标题 | 文章组织 | 文章日期 | 文章网址
articles_tags
:
编号 | article_id | tag_id | 文章标签
我想我需要以某种方式获取文章ID 和标签ID 并将它们插入到第二个表中(?)。
这需要第三张桌子吗?
我什至在正确的轨道上吗?