【问题标题】:Because when selecting several categories per checkbox only one is shown in the database因为当每个复选框选择多个类别时,数据库中只显示一个
【发布时间】:2020-12-22 05:13:44
【问题描述】:

我目前正在写一个php和mysql的小博客,发生的情况是当我创建一个新的POST或发布时,我需要选择几个类别的帖子。我设法创建类别系统,但在选择几个时,只有1显示在数据库中。

如您所见,我选择了几个类别,但在查看 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

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

【问题讨论】:

  • 如果 categories 是 topic_id,则创建一个名为 post_topics 的新表,将 topic_id 和 post_id 添加为列,然后在插入时将所有选定的 id 添加为行,然后在选择时查询主题并加入表以获取主题的字符串值,它称为一对多,是一种非常常见的结构。

标签: php html mysql checkbox


【解决方案1】:

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

【讨论】:

  • 我已经尝试过了,但是在创建出版物时它告诉我 topic_id: 0
  • 它为 0,因为数组不是 int 数据类型
  • @LawrenceCherone 我该如何解决?
  • 使用我上面评论中提到的一对多结构。或者不推荐并且您没有收到我的消息,将其更改为 TEXT 并使用 json_encode
猜你喜欢
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-10
  • 1970-01-01
相关资源
最近更新 更多