我的博客最初是在发帖,但是,当您处理事情时,事情就会中断。现在我一辈子都弄不明白为什么什么都没有发生!类别发布到数据库就好了,但我的帖子拒绝。这是我的东西
<?php
include_once('header.php');
if(isset($_POST['title'], $_POST['contents'], $_POST['category'])){
$errors = array();
if(!empty(trim($_POST['title']))){
$title = trim($_POST['title']);
}else{
$errors[] = 'Please enter title';
}
if(!empty(trim($_POST['contents']))){
$contents = trim($_POST['contents']);
}else{
$errors[] = 'You need to supply the content';
}
if(!category_id_exists($_POST['category'])){
$errors[] = 'That category does not exist.';
}
if(strlen(trim($_POST['title']))>255){
$errors[] = 'The title cannot be longer than 255 characters.';
}
if(empty($errors)){
add_post($title, $contents, $_POST['category']);
$id = mysqli_insert_id($mysql_connect);
$header_string = 'Location: index.php?id='.$id;
header($header_string);
die();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<style>
label { display: block;}
</style>
<title>Add a Post</title>
</head>
<body>
<h1> Add a Post </h1>
<?php
if(isset($errors) && !empty($errors)){
echo '<ul><li>', implode('</li><li>', $errors), '</li></ul>';
}
?>
<form action="" method="post">
<div>
<label for="title"> Title </label>
<input type="text" name="title" value="<?php if(isset($_POST['title'])) echo $_POST['title']; ?>">
</div>
<div>
<label for="contents"> Contents </label>
<textarea name="contents" rows="15" cols="50"><?php if(isset($_POST['contents'])) echo $_POST['contents']; ?></textarea>
</div>
<div>
<label for="category"> Category </label>
<select name="category">
<?php
foreach(get_categories() as $category){
?>
<option value="<?php echo $category['id']; ?>"><?php echo $category['name']; ?> </option>
<?php
}
?>
</select>
</div>
<div>
<input type="submit" value="Add Post">
</div>
</form>
</body>
</html>
在 header.php 文件中,我调用了一个 init.php 连接到 db 文件,该文件本身也包括了我的博客 (blog.php) 的所有功能。具体需要的函数如下……
function add_post($title, $contents, $category){
global $mysql_connect;
$title = mysqli_real_escape_string($mysql_connect, $title);
$contents = mysqli_real_escape_string($mysql_connect, $contents);
$category = (int)$category;
$result = mysqli_query($mysql_connect, "INSERT INTO posts SET cat_id = '$category', title = '$title', contents = '$contents', date_posted = NOW()");
}
我认为现在,一双全新的眼睛对我来说真的很有值(value)。当我点击添加帖子时,它会将我带到标题为 http://localhost/blog/index.php?id=0 的页面.因此,它不仅没有注册到数据库,而且也没有使用类别。
希望我的解释没问题!我已经在这里工作了一个星期了。
编辑:我运行了错误检查但没有任何帖子,所以我不确定发生了什么!
Edit2:选择我添加的类别,也可以按照我希望的方式列出。当我点击添加帖子时,它只是发生了一些事情,没有任何反应。我假设它在这个区域
if(empty($errors)){
add_post($title, $contents, $_POST['category']);
$id = mysqli_insert_id($mysql_connect);
$header_string = 'Location: index.php?id='.$id;
header($header_string);
die();
}
最佳答案
与任何问题一样,它总是大海捞针!我通过 MYSQL 工作台运行代码,发现这是我对不需要关系的外键的设置。现在都修好了!
关于php - 为什么我的博客发不了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49838131/