作为一项学校作业,我需要制作一个cms,因为我需要能够对其进行编辑并删除它们。所以我可以编辑和删除它们,但由于某种原因,我不能让它插入帖子(以及类别,几乎相同)我希望你们可以帮助我。
以下是代码:
表格
<form action="includes/doAddpost.php" method="post">
<label for="PostName">Name</label>
<input type="text" name="PostName" id="PostName" placeholder="Title" autofocus="auto"/>
<label for="PostAuthor">Author</label>
<input type="text" name="PostAuthor" id="PostAuthor" placeholder="Authors name"
value="<?php if (isset($_SESSION['username'])) {
echo $_SESSION['username'];
}
?>"/>
<label for="PostContent">Content</label>
<textarea name="PostContent" id="PostContent" placeholder="content"></textarea>
<label for="PostCats">category</label>
<select name="PostCats">
<?php
$query = "SELECT * FROM categories";
$result = mysqli_query($mysqli, $query);
while ($cat = mysqli_fetch_assoc($result)) {
?>
<option value="<?php echo $cat['id']; ?>"><<?php echo $cat['title']; ?></option>
<?php } ?>
这部分似乎无法正常工作
</select>
<input type="submit" name="submit" value="submit"/>
</form>
这是doAddpost页面:
<?php
include '../../includes/functions.php';
sec_session_start();
if(isset($_POST['submit'])){
if(isset($_POST['PostName'])){
if(isset($_POST['PostContent'])){
addPost($mysqli,$_POST['PostName'],$_POST['PostAuthor'], $_POST['PostContent'],$_POST['PostCats']);
header("Location: ../posts.php");
}else{
echo"please enter some content!";
}
} else{
echo"please set a category name!";
include('../addpost.php');
}
}else{
header("Location: ../addpost.php");
}
?>
和功能:
function addPost($mysqli, $pName, $pAuthor, $pContent, $pCat = 1)
{
$query = "INSERT INTO posts VALUES ('$pName', '$pAuthor', '$pContent', $pCat)";
mysqli_query($mysqli, $query);
}
谁能告诉我我面临的问题是什么?
答案 0 :(得分:0)
只需将您的功能编辑为
即可function addPost($mysqli, $pName, $pAuthor, $pContent, $pCat = 1)
{
$query = "INSERT INTO posts (`your_column1`, `your_column_2`, `your_column_3`, `your_column_4`) VALUES ('$pName', '$pAuthor', '$pContent', $pCat)";
mysqli_query($mysqli, $query) or die(mysqli_error());
}
然后尝试......
同样在你选择列表中将其更改为,
<option value="<?php echo $cat['id']; ?>"><?php echo $cat['title']; ?></option>
你在代码中放了一个额外的<
..检查......:)
答案 1 :(得分:0)
现在是时候一步一步调试了: -
1)为调试目的更改您的选择类别mysqli_query,如下所示
mysqli_query( $mysqli , $query ) or trigger_error($mysqli->error."($query)");
2)为您插入要在其中插入记录的查询提及列名称。正如您在评论中提到的那样,您不希望id为null,因此您应该将id列设为AUTOINCREMENT
例如
INSERT INTO posts (`column1`,`column2`,`column3`,`column4`) VALUES ('$pName', '$pAuthor', '$pContent', $pCat);