我有一個使用複選框的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('','','','','');
}
?>