我有一个使用复选框的PHP表单。我也有一个有3个表的MySQL数据库。处理PHP表单复选框到MySQL表中的问题
其中一个表名为TAGS,其列为ID和ARTICLE_CONTENTS。
数据库中的另一个表称为ARTICLES,其列为ID,ARTICLETITLE,ARTICLEORGANIZATION,ARTICLEDATE和ARTICLEURL。
第三个表称为ARTICLES_TAGS其列ARTICLE_ID和TAG_ID
标记表中有87项是类似于:
1 | geology
2 | astronomy
3 | chemistry
数据库的目的是创造之间的关系标签和文章。为此,PHP表单使用复选框,用户可以在将新条目添加到数据库时进行检查。这些复选框代表TAGS表中的标签。因此,例如,对于TAGS表中的每个条目都会有一个复选框:[] geology [] astronomy [] chemistry ... etc ...
我想要做的是插入信息文本框(文章标题,文章组织,文章日期和文章url),并使用mysql_insert_id()获取该插入的ID,并将该ID与与被选中的复选框相关联的标记的ID配对。
所以,举例来说,如果地质复选框将被检查,如果在标签表地质项分别是:
02 | geology
而且,如果被插入的文章ID发生是142
然后
一个新的条目将被插入到ARTICLES_TAGS:
Article_ID | TAG_ID
142 | 02
但是,无论何时执行我的表单,我都不会在ARTICLES_TAGS表中获得任何条目,尽管这些信息会正确插入到ARTICLES表中。我无法弄清楚我出错的地方。
我一直在研究这个问题的措辞几天,我想现在很清楚。请让我知道是否需要澄清。
的代码是:
<?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="76%" border="0" cellpadding="6">
<tr>
<td colspan="2"><legend></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="1" id="articletags_0" />Science
<input type="checkbox" name="articletags[]" value="2" id="articletags_1" />Geology
</span></td>
</tr>
<tr>
<td colspan="2" align="center" valign="middle"><input type="submit" name="submit" value="Add this Article" /></td>
</tr>
</table>
</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']));
{
}
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' ");
$article_id = mysql_insert_id();
foreach ($_POST['articletags'] as $newtag)
{
mysql_query(" INSERT INTO articles_tags article_id='$article_id',
tag_id='$newtag' ");
}
header("Location:addsuccess.php");
}
}
else
{
renderForm('','','','','');
}
?>