Error uploading data to DB in CodeIgniter

0

Basically I have the following in my controller:

    $title_input = strip_tags($this->input->post('title'));
    $slug = str_replace(' ', '-', $title_input);
    $slug = strtolower($slug);

    if($title_input != ''){
        $data = array(
            'title' => $title_input,
            'slug'  => $slug,
        );
    } else {
        $data = array(
            'title' =>  'Untitled',
            'slug'  =>  'untitled',
        );
    }

    $data = array(
        'body'          => strip_tags($this->input->post('body')),
    );

    if($post_image != ''){
        $data['post_image'] = $post_image;
    }

    $this->Post_model->add($data);

What I'm trying to do is upload information through a Modal where I have three inputs, which are the following:

  • Title
  • Body
  • Post_image

And the problem I'm having is that if the user chooses to leave the title input empty, it should go up as 'Untitled' value and in its slug the same 'untitled' but if the input of the title value is not empty, then you should upload the specified information.

The current behavior when leaving empty title input, does not upload anything (no title, no body, no post_image) but when I write something in that input (title) the body does not upload (but the post_image and the title yes) ... to make it clearer:

empty title input:

  • Do not upload anything to DB

input title not empty:

  • title
  • no body
  • post_image

and what I want to leave the input title empty but not the body:

  • untitled
  • body
  • post_image

UPDATE: This is what I have in my view

            <?php $args = array(
                'id'    => 'user_form',
            ) ?>
            <?= form_open_multipart('users/newmedia', $args); ?>
                <!-- Title -->
                <div class="form-group">
                    <?= form_label('Title:', 'title'); ?>
                    <?php $data = array(
                        'id'    =>  'title',
                        'name'  =>  'title',
                        'class' =>  'form-control',
                        'value' =>  set_value('title'),
                    ); ?>
                    <?= form_input($data); ?>
                </div>
                <!-- Description -->
                <div class="form-group">
                    <?= form_label('Description:', 'body'); ?>
                    <?php $data = array(
                        'id'    =>  'body',
                        'name'  =>  'body',
                        'class' =>  'form-control',
                        'value' =>  set_value('body'),
                        'style' =>  'max-width: 100%'
                    ); ?>
                    <?= form_textarea($data); ?>
                </div>
                <!-- File Input -->
                <div class="input-group">
                    <label class="input-group-btn">
                        <span class="btn btn-warning">
                            Browse 
                            <?php $data = array(
                                    'id'    =>  'post_image',
                                    'name'  =>  'post_image',
                                    'class' =>  'form-control',
                                    'value' =>  set_value('post_image'),
                                    'style' =>  'display:none',
                                ); ?>
                                <?= form_upload($data, '', ''); ?>
                        </span>
                    </label>
                    <input type="text" class="form-control" readonly>
                </div>
                <!-- Submit -->
                <hr>
                <?php $args = array(
                    'id'    =>  'mysubmit',
                    'class' =>  'btn btn-warning btn-sm'
                ); ?>
                <?= form_submit('mysubmit', 'Create', $args); ?>

            <?= form_close(); ?>

Nose if I explain myself but I hope you help me with this little query that I want to create, thank you in advance.

    
asked by Kirasiris 17.09.2018 в 08:31
source

1 answer

0

The possibility that occurs to me is that you can be treading the variable $data and that is why at some point you delete the previous data. I recommend creating the variable $data at once.

$title_input = strip_tags($this->input->post('title'));
$body = strip_tags($this->input->post('body'));
$post_image = $this->input->post('post_image');
    $slug = str_replace(' ', '-', $title_input);
    $slug = strtolower($slug);

    if($title_input != ''){
        $data = array(
            'title' => $title_input,
            'slug'  => $slug,
            'body'  =>  $body,
            if($post_image != ''){
             'post_image'  =>  $post_image,
        );
    } else {
        $data = array(
            'title' =>  'Untitled',
            'slug'  =>  'untitled',
            'body'  =>  $body,
            if($post_image != ''){
             'post_image'  =>  $post_image,
            }
        );
    }

    $this->Post_model->add($data);
    
answered by 17.09.2018 в 09:43