首页 > 解决方案 > 因为当每个复选框选择多个类别时,数据库中只显示一个

问题描述

我目前正在写一个 php 和 mysql 的小博客,发生的情况是,当我创建一个新的 POST 或发布时,我需要选择几个类别的帖子。I managed to create the category system but when selecting several, only 1 is shown in the database.

我走了

如您所见,我选择了几个类别,但是在查看 Mysql 时,仅选择了 1 个类别,在这种情况下仅选择 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, $id, $_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

并且所有类别都显示在数据库中,请有人可以帮助我

标签: phphtmlmysqlcheckbox

解决方案


您需要更改name="topic_id"为,name="topic_id[]"以便将其视为一个数组,并且服务器将接受该输入的多个值。


推荐阅读