2
我有一個addpost.php,它有一個表單提交博客文章到mysql數據庫。該表是: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表。以下是表單字段的示例。
<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>
所以,如果按下提交按鈕,插入語句得到執行:
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;
什麼是真正發生什麼還怎麼類別名稱和複選框得到來自查詢blog_cats表顯示傳遞到blog_post_cats表!所以我不確定發生了什麼問題?
我覺得INSERT語句插入的CATID和對帖子ID的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>
讚賞任何幫助。