Send values to the database

Asked

Viewed 78 times

1

You can help me send the values of the fields 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>

Php code:

<?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 latest 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.

inserir a descrição da imagem aqui

  • Just try "UPDATE nota SET titulo = 'NOTE 4', nota = 'TEXT NOTE 4', usuario_nome = 'Wagner Fillio', usuario_id = '1', dt_alteracao = 1482159904 WHERE id = 4"

  • Thanks, I did it like this: UPDATE noteSETtitle= 'NOTA 4',note= 'TEXTO NOTA 4',username= 'Wagner Fillio',user name= '1',dt_amendment= 1482160549 WHEREid = '4' Will not! Only take ID 4, even if I pass ID 2

  • The problem must be this: ".. SET id = '4',..." erases this "id = '4'.... Do as I said above, without taking or putting anything... THUS: "UPDATE SET NOTE title = 'NOTE 4', note = 'TEXT NOTE 4', usuario_name = 'Wagner Fillio', usuario_id =1, dt_change = 1482159904 WHERE id = 4"

  • Sorry, I passed this way, updated the return

  • The form tag is before Include, I don’t know if there’s a problem with this, but I tried to put the form where the fields are, but it didn’t pass.

  • You tried this SQL query I put on top?

  • Yes, the same way and it still won’t. Always updates ID 4, and I’m passing another ID in the post.

  • Ha then that’s what’s wrong, does: "UPDATE NOTE SET title = 'NOTE 4', note = 'TEXT NOTE 4', usuario_name = 'Wagner Fillio', usuario_id =1, dt_alteracao = 1482159904 WHERE id = ID_QUERES_AQUI"... This last part put the right id

  • It looks like this: Check out the controller: $id = $this->input->post('id'); And look at the field: <input type=\"text\" class=\"form-control\" rows=\"14\" name=\"id\" placeholder=\"Título\" value=\"" . $row['id'] . "\">

  • Yes, then when you submit that form you will need to grab that id that comes from the form and put it in that SQL query

  • But this should already work like this, since name='field name' sends the field values to the controller. Is sending, but always ID 4, if it had 5 record in the database, would be sent ID 5.

Show 6 more comments

1 answer

0

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

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

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

This way you will use a for() loop to save the information in the database.

Hug,

Browser other questions tagged

You are not signed in. Login or sign up in order to post.