mercredi 2 septembre 2020

Because when selecting several categories per checkbox only one is shown in the database

I'm currently working on a small blog in php and mysql, what happens is that when I create a new POST or publication, I need to select several categories of the post. I managed to create the category system but when selecting several, only 1 is shown in the database.

IMG ONE

As you can see, I have selected several categories but when reviewing the Mysql only 1 has been selected in this case the Adventure category only.

IMG TWO

CODE: 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>

CODE: 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'];
}
} 
?>

CODE: 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'];
}
} 
?>    

I need to create that when selecting several checkboxes, these are registered in the database for example when creating a publication called

THE OCEAN | CATEGORIES: EXAMPLE 1, EXAMPLE 2

and that all the categories are shown in the database, someone can help me please




Aucun commentaire:

Enregistrer un commentaire