Send values to the database

1

They can help me to send the field values to the database.

For the viewer, I'm using the following code:

Page Code:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

?>

            <div class="content-wrapper">
                <section class="content-header">
                    <?php echo $pagetitle; ?>
                    <?php echo $breadcrumb; ?>
                </section>

                <section class="content">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="box">
                                <!--
                                <div class="box-header with-border">
                                    <h3 class="box-title">Bloco de Notas</h3>
                                </div>
                                -->
                                <div class="box-header with-border">
                                    <form method="get" action="<?= base_url()?>admin/usuarios">                                 
                                        <div class="input-group pull-left">
                                            <h3 class="box-title"><?php echo anchor('admin/notas/adicionar', '<i class="fa fa-plus"></i> '. 'Adicionar Nota', array('class' => 'btn btn-block btn-primary btn-flat')); ?></h3>
                                        </div>
                                    </form>
                                </div>                              
                                    <div class="box-body">
                                    <form action="<?= base_url()?>admin/notas/salvar" id="" method="post">
                                        <?php include 'notas.php';?>
                                        <div class="form-group">
                                            <div class="box-header" style="padding-left: 0px">  
                                                <?php echo form_button(array('type' => 'submit', 'class' => 'btn btn-primary btn-flat', 'content' => lang('actions_submit'))); ?>
                                                <?php echo form_button(array('type' => 'reset', 'class' => 'btn btn-warning btn-flat', 'content' => lang('actions_reset'))); ?>
                                                <?php echo anchor('admin/dashboard', lang('actions_cancel'), array('class' => 'btn btn-default btn-flat')); ?>
                                            </div>
                                        </div>
                                    </form>
                                    </div>                              
                            </div>
                        </div>
                    </div>
                </section>
            </div>

Code php:

<?php

    $classActive = "";
    $divMenu = "";
    $divPanel = "";

    $this->db->order_by('id', 'asc');
    $this->db->where('usuario_id', $this->session->userdata('id'));
    $this->db->where('usuario_nome', $this->session->userdata('usuario_nome'));
    $nota = $this->db->get('nota')->result_array();
    $contador = 0;
    foreach ($nota as $row) {
        $classActive .= ($contador == 0) ? "active" : "inactive";
        //$divMenu = "<li class=\"" . $classActive . "\"><a href=\"#" . $row['id'] . "\" data-toggle=\"tab\"><i class=\"\"></i>" . $row['titulo'] . "</a></li>";
        $divPanel .= "
        <div class=\"tab-pane " . $classActive . "\" id=\"" . $row['id'] . "\">         
            <div id=\"sample\" class=\"ruledpaper\">
                <div class=\"form-group\" style=\"margin: 0px;\">
                    <div class=\"col-md-12\" style=\"padding:0px; background-color: #FFFCEE; font-size: 5px;\">
                        <input type=\"text\" class=\"form-control\" rows=\"14\" name=\"id\" placeholder=\"Título\" value=\"" . $row['id'] . "\">
                        <input type=\"text\" class=\"form-control\" rows=\"14\" style=\"padding: 5px; border:0px; background-color: #fff6cc; font-size: 18px;\" name=\"titulo\" placeholder=\"Título\" value=\"" . $row['titulo'] . "\">
                    </div>
                </div>
                <hr style=\"margin: 0px;\" />
                <div class=\"form-group\">
                    <div class=\"col-md-12\" style=\"padding:0px;\">
                        <textarea maxlength=\"60\" class=\"ruledpaper form-control\" rows=\"\" cols=\"\" style=\"padding: 5px; border:0px; min-height: 350px;\" name=\"nota\" value=\"" . $row['nota'] ."\" placeholder=\"Digite o texto...\">" . $row['nota'] . "</textarea>
                    </div>
                </div>
            </div>
        </div>";
        $contador++;
    }
?>


<div class="row">
    <div class="col-sm-8">
        <div class="tab-content" style="width: 70%;">
            <?php echo $divPanel; ?>            
        </div>
    </div>
    <div class="col-sm-4">
        <ul class="nav tabs-vertical">
        <?php foreach ($nota as $row){?>
            <li class="">           
                <a href="#<?php echo $row['id'];?>" data-toggle="tab"> <i class=""></i> <?php echo $row['titulo'];?> </a>               
            </li>           
        <?php }?>
        </ul>       
    </div>
</div>

<style>
.ruledpaper {
  line-height: 2em;
  background: #ffffee -webkit-gradient(linear, left top, left bottom, from(rgba(0, 0, 0, 0)), color-stop(0.96, rgba(0, 0, 0, 0)), color-stop(0.98, rgba(0, 0, 0, 0.5)), to(rgba(0, 0, 0, 0)));
  background-size: auto 2em;
  overflow: hidden;
  padding: 0em;
  border: solid 0.5em #ffffee;
  -webkit-box-shadow: 0.2em 0.2em 0.2em silver; }

#sample {
  width: 57em;
  height: 25em;
  font-size: 15px; }
</style>

Controller

 $id = $this->input->post('id');
 $data['id']                       =   $id;
 $data['titulo']                   =   $this->input->post('titulo');
 $data['nota']                     =   $this->input->post('nota');
 $data['usuario_nome']             =   $this->session->userdata('nome_usuario');
 $data['usuario_id']               =   $this->session->userdata('id');
 $data['dt_alteracao'] =  strtotime(date("d-m-Y H:i:s"));           

 $this->db->where('id',$id);

 if($this->db->update('nota', $data))

However, it is being sent to the database, always the last ID.

UPDATE 'nota' SET 'id' = '4', 'titulo' = 'NOTA 4', 'nota' = 'TEXTO NOTA 4',
  'usuario_nome' = 'Wagner Fillio', 'usuario_id' = '1', 
  'dt_alteracao' = 1482159904 WHERE 'id' = '4'

Being that I want to send only the selected ID.

    
asked by anonymous 19.12.2016 / 16:08

1 answer

0

Do the following, remove the form from the page and have each note have its own form, to do this add the form tags inside foreach (), so when the user clicks to save note 1 it will save the form of note 1, when you save note 2 it will save the form of note 2, this way you created it, you are telling the code that everything inside that form is a single note.

In case you want the system to save all the notes in a single form as it is now, the name of the inputs should look like this:

<input name="nota[]">
<input name="descricao[]">

In this way you will use a for () loop to perform the saving of the information in the database.

Hugs,

    
23.01.2017 / 00:19