我有一个addpost.php,它有一个将博客文章提交到mysql数据库的表单。表格是:
blog_post_cats
+--------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| postID | int(11) | YES | | NULL | |
| catID | int(11) | YES | | NULL | |
+--------+------------------+------+-----+---------+----------------+
blog_posts_seo
+-----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+----------------+
| postID | int(11) unsigned | NO | PRI | NULL | auto_increment |
| postTitle | varchar(255) | YES | | NULL | |
| postDesc | text | YES | | NULL | |
| postCont | text | YES | | NULL | |
| postDate | datetime | YES | | NULL | |
+-----------+------------------+------+-----+---------+----------------+
blog_cats
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| catID | int(11) unsigned | NO | PRI | NULL | auto_increment |
| catTitle | varchar(255) | YES | | NULL | |
+----------+------------------+------+-----+---------+----------------+
当用户提交表单时,帖子标题,内容,描述会发布到blog_posts_seo表,但所选类别也会将catID和postID传递给blog_post_cats表。下面是表单字段的示例。此外,如何通过查询blog_cats表显示类别名称和复选框:
<form action='' method='post'>
<p><label>Title</label><br />
<input type='text' name='postTitle' value='<?php if(isset($error)){ echo $_POST['postTitle'];}?>'></p>
<p><label>Description</label><br />
<textarea name='postDesc' cols='60' rows='10'><?php if(isset($error)){ echo $_POST['postDesc'];}?></textarea></p>
<p><label>Content</label><br />
<textarea name='postCont' cols='60' rows='10'><?php if(isset($error)){ echo $_POST['postCont'];}?></textarea></p>
<fieldset>
<legend>Categories</legend>
<?php
$stmt2 = $db->query('SELECT catID, catTitle FROM blog_cats ORDER BY catTitle');
while($row2 = $stmt2->fetch()){
if(isset($_POST['catID'])){
if(in_array($row2['catID'], $_POST['catID'])){
$checked="checked='checked' ";
}else{
$checked = null;
}
}
echo "<input type='checkbox' name='catID[]' value='".$row2['catID']."' $checked> ".$row2['catTitle']."<br />";
}
?>
</fieldset>
<p><input type='submit' name='submit' value='Submit'></p>
</form>
因此,如果按下提交按钮,则执行insert语句:
try {
//insert into database
$stmt = $db->prepare('INSERT INTO blog_posts_seo (postTitle,postDesc,postCont,postDate) VALUES (:postTitle, :postDesc, :postCont, :postDate)') ;
$stmt->execute(array(
':postTitle' => $postTitle,
':postDesc' => $postDesc,
':postCont' => $postCont,
':postDate' => date('Y-m-d H:i:s')
));
$postID = $db->lastInsertId();
//add categories
if(is_array($catID)){
foreach($_POST['catID'] as $catID){
$stmt = $db->prepare('INSERT INTO blog_post_cats (postID,catID)VALUES(:postID,:catID)');
$stmt->execute(array(
':postID' => $postID,
':catID' => $catID
));
}
}
//redirect to index page
header('Location: index.php?action=added');
exit;
实际发生的事情是将NOTHING传递给blog_post_cats表!!所以我不确定会出现什么问题?
我认为插入语句对于将catID和postID插入blog_post_cats表是正确的,所以我怀疑以下是错误的:
<fieldset>
<legend>Categories</legend>
<?php
$stmt2 = $db->query('SELECT catID, catTitle FROM blog_cats ORDER BY catTitle');
while($row2 = $stmt2->fetch()){
if(isset($_POST['catID'])){
if(in_array($row2['catID'], $_POST['catID'])){
$checked="checked='checked' ";
}else{
$checked = null;
}
}
echo "<input type='checkbox' name='catID[]' value='".$row2['catID']."' $checked> ".$row2['catTitle']."<br />";
}
?>
</fieldset>
任何帮助表示感谢。
答案 0 :(得分:0)
经过大量测试后,我发现了为什么catID和postID没有插入的问题......
if(isset($_POST['submit'])){
$_POST = array_map( 'stripslashes', $_POST );
//collect form data
extract($_POST);
我没有在我的问题中包含此代码,我发现如果我注释掉了:
/*$_POST = array_map( 'stripslashes', $_POST );
//collect form data
extract($_POST);*/
... catID和postID正确插入数据库。
似乎我将$ _POST数组的值发送到stripslashes函数,但它阻止了数据插入到数据库中。