如何解决因为当每个复选框选择多个类别时,数据库中仅显示一个类别
我目前正在使用php和mysql创建一个小型博客,发生的事情是,当我创建新的POST或发布时,我需要选择几类帖子。我设法创建类别系统,但选择多个时,仅1在数据库中被示出。
如您所见,我选择了几个类别,但是在查看Mysql时,在这种情况下,仅选择了Adventure类别。
代码:create.php
<form action="create.php" method="post">
<div class="input-group">
<label>Nombre</label>
<input type="text" name="title" value="<?php echo $title; ?>" class="text-input">
</div>
<div class="input-group">
<label>Body</label>
<input type="text" name="body" value="<?php echo $body; ?>" class="text-input">
</div>
<div class="box">
<?php foreach ($categorias as $key => $categoria): ?>
<?php if(!empty($topic) && $topic == $topic['id']): ?>
<input id="one" selected value="<?php echo $categoria['id']; ?>" type="checkbox" name="topic_id">
<span class="check"></span>
<label for="one"><?php echo $categoria['name']; ?></label>
<?php else: ?>
<input id="one" selected value="<?php echo $categoria['id']; ?>" type="checkbox" name="topic_id">
<span class="check"></span>
<label for="one"><?php echo $categoria['name']; ?></label>
<?php endif; ?>
<?php endforeach; ?>
</div>
代码:posts.php
<?php
include($_SERVER['DOCUMENT_ROOT'].'/app/database/db.php');
include($_SERVER['DOCUMENT_ROOT'].'/app/helpers/validatePost.php');
$table = 'posts';
$categorias = selectAll('categorias');
$errors = array();
$id = '';
$title = '';
$body = '';
$topic_id = '';
$posts = selectAll($table);
if (isset($_POST['add-post'])) {
$errors = validatePost($_POST);
if (count($errors) === 0){
unset($_POST['add-post']);
$post_id = create($table,$_POST);
$_SESSION['message'] = 'Capitulo creado correctamente';
$_SESSION['type'] = 'success';
header('location: ../../admin/posts/index.php');
exit();
} else {
$title = $_POST['title'];
$body = $_POST['body'];
$topic = $_POST['topic_id'];
}
}
if (isset($_GET['id'])){
$id = $_GET['id'];
$post = selectOne($table,['id' => $id]);
$id = $post['id'];
$title = $post['title'];
$body = $post['body'];
$topic = $post['topic_id'];
}
if (isset($_GET['del_id'])){
$id = $_GET['del_id'];
$count = delete($table,$id);
$_SESSION['message'] = 'Capitulo eliminado correctamente';
$_SESSION['type'] = 'success';
header('location: ../../admin/posts/index.php');
exit();
}
if (isset($_POST['update-post'])){
$errors = validateEdit($_POST);
if (count($errors) === 0){
$id = $_POST['id'];
unset($_POST['update-post'],$_POST['id']);
$post_id = update($table,$id,$_POST);
$_SESSION['message'] = 'Genero actualizado correctamente';
$_SESSION['type'] = 'success';
header('location: ../../admin/topics/index.php');
exit();
} else {
$id = $_POST['id'];
$title = $_POST['title'];
$body = $_POST['body'];
$topic = $_POST['topic_id'];
}
}
?>
代码:topics.php
<?php
include($_SERVER['DOCUMENT_ROOT'].'/app/database/db.php');
include($_SERVER['DOCUMENT_ROOT'].'/app/helpers/validateCategoria.php');
$table = 'categorias';
$errors = array();
$id = '';
$name = '';
$categorias = selectAll($table);
if (isset($_POST['add-topic'])) {
$errors = validateCategoria($_POST);
if (count($errors) === 0){
unset($_POST['add-topic']);
$topic_id = create($table,$_POST);
$_SESSION['message'] = 'Genero creado correctamente';
$_SESSION['type'] = 'success';
header('location: ../../admin/topics/index.php');
exit();
} else {
$name = $_POST['name'];
}
}
if (isset($_GET['id'])){
$id = $_GET['id'];
$categoria = selectOne($table,['id' => $id]);
$id = $categoria['id'];
$name = $categoria['name'];
}
if (isset($_GET['del_id'])){
$id = $_GET['del_id'];
$count = delete($table,$id);
$_SESSION['message'] = 'Genero eliminado correctamente';
$_SESSION['type'] = 'success';
header('location: ../../admin/topics/index.php');
exit();
}
if (isset($_POST['update-topic'])){
$errors = validateCategoria($_POST);
if (count($errors) === 0){
$id = $_POST['id'];
unset($_POST['update-topic'],$_POST['id']);
$topic_id = update($table,$_POST);
$_SESSION['message'] = 'Genero actualizado correctamente';
$_SESSION['type'] = 'success';
header('location: ../../admin/topics/index.php');
exit();
} else {
$id = $_POST['id'];
$name = $_POST['name'];
}
}
?>
我需要创建选择多个复选框时,这些在数据库例如注册创建发布时调用
海洋|分类:示例1,示例2
所有类别都显示在数据库中,请有人可以帮助我
解决方法
您需要将name="topic_id"
更改为name="topic_id[]"
,以便将其视为数组,并且服务器将接受该输入的多个值。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。