gpt4 book ai didi

PHP 记录未插入到 MySql 博客类别表中

转载 作者:行者123 更新时间:2023-11-28 02:56:25 28 4
gpt4 key购买 nike

我有一个 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 | |
+-----------+------------------+------+-----+---------+----------------+

博客猫

+----------+------------------+------+-----+---------+----------------+
| 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>

因此,如果提交按钮被按下,插入语句就会被执行:

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_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>

感谢任何帮助。

最佳答案

经过多次测试,我弄清楚了为什么 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 函数,但它阻止了数据插入数据库。

关于PHP 记录未插入到 MySql 博客类别表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36844427/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com