Codeigniter,我无法将表单值提交到数据库中

时间:2013-07-08 14:30:02

标签: php mysql codeigniter

我通过了4个多小时寻找错误的地方。但没有找到 我的观点inscriresalle.php:

<div class="widget-box">
    <div class="widget-title">
    <span class="icon">
        <i class="icon-align-justify"></i>                                  
    </span>
    <h5><?php echo empty($participant_salle->id) ? 'Nouveau Agent OCP:' : 'Modification de: ' . $participant_salle->nom.' '.$participant_salle->prenom; ?></h5>
    </div>

    <div class="widget-content nopadding">
    <div class="form-horizontal" name="basic_validate" id="basic_validate" novalidate="novalidate">
    <?php echo form_open(); ?>
      <div <?php if(form_error('matricule')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
          <label class="control-label">Matricule :</label>
          <div class="controls">
              <?php echo form_input('matricule', set_value('matricule', $this->input->get('matricule'))); ?>
              <span class="help-inline"><?php echo form_error('matricule'); ?></span>
          </div>
      </div>
      <div  <?php if(form_error('nom')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
        <label class="control-label">Nom :</label>
        <div class="controls">
           <?php echo form_input('nom', set_value('nom', $this->input->get('nom'))); ?>
           <span class="help-inline"><?php echo form_error('nom'); ?></span>
        </div>
      </div>
      <div  <?php if(form_error('prenom')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
        <label class="control-label">Prénom :</label>
        <div class="controls">
           <?php echo form_input('prenom', set_value('prenom', $this->input->get('prenom'))); ?>
           <span class="help-inline"><?php echo form_error('prenom'); ?></span>
        </div>
      </div>


      <div  <?php if(form_error('beneficiaire')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
        <label class="control-label">Bénéficiaire :</label>

        <div class="controls">
            <label><?php echo form_radio('beneficiaire', 'Agent', $this->input->get('beneficiaire') == 'Agent') ?> Agent </label>
            <label><?php echo form_radio('beneficiaire', 'Conjoint', $this->input->get('beneficiaire') == 'Conjoint') ?> Conjoint </label>
            <label><?php echo form_radio('beneficiaire', 'Enfant', $this->input->get('beneficiaire') == 'Enfant') ?> Enfant </label>
            <span class="help-inline"><?php echo form_error('beneficiaire'); ?></span>
        </div>
      </div>

      <div  <?php if(form_error('sexe')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
        <label class="control-label">Sexe :</label>
        <div class="controls">

                    <label><?php echo form_radio('sexe', 'M', $this->input->get('sexe') == 'M') ?> Masculin </label>

                    <label><?php echo form_radio('sexe', 'F', $this->input->get('sexe') == 'F') ?> Féminin </label>
                    <span class="help-inline"><?php echo form_error('sexe'); ?></span>

        </div>
      </div>


      <div  <?php if(form_error('date_naissance')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
        <label class="control-label">Date de Naissance :</label>
        <div class="controls">
           <input type="text" name="date_naissance" id="date1" value="<?php echo set_value('date_naissance', $this->input->get('dateNaissance')); ?>" />
           <span class="help-inline"><?php echo form_error('date_naissance'); ?></span>
        </div>
      </div>


      <div  <?php if(form_error('telephone')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
        <label class="control-label">Téléphone :</label>
        <div class="controls">
           <?php echo form_input('telephone', set_value('telephone', $participant_salle->telephone)); ?>
           <span class="help-inline"><?php echo form_error('telephone'); ?></span>
        </div>
      </div>



      <div  <?php if(form_error('date_inscription_salle')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
        <label class="control-label">Date inscription :</label>
        <div class="controls">
           <input type="text" name="date_inscription_salle" id="date2" value="<?php echo set_value('date_inscription_salle', $participant_salle->date_inscription_salle); ?>" />
           <span class="help-inline"><?php echo form_error('date_inscription_salle'); ?></span>
        </div>
      </div>

      <div  <?php if(form_error('salle_debut_periode')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
        <label class="control-label">Début période :</label>
        <div class="controls">
          <input type="text" name="salle_debut_periode" id="date3" value="<?php echo set_value('salle_debut_periode', $participant_salle->salle_debut_periode); ?>" />
          <span class="help-inline"><?php echo form_error('salle_debut_periode'); ?></span>
        </div>
      </div>


      <div  <?php if(form_error('salle_fin_periode')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
        <label class="control-label">Fin période :</label>
        <div class="controls">
          <input type="text" name="salle_fin_periode" id="date4" value="<?php echo set_value('salle_fin_periode', $participant_salle->salle_fin_periode); ?>" />
          <span class="help-inline"><?php echo form_error('salle_fin_periode'); ?></span>
          <span class="help-block">


        </div>
      </div>


<script type="text/javascript">// <![CDATA[
var qp = new Array();
var tarif = new Array();
var datestart = document.getElementById('date3');
var dateend = document.getElementById('date4');

$(document).ready(
// a chaque changement de discipline on obtien l'id du tableau discipline.
function(){ 

$('#country').change(function(){ //any select change on the dropdown with id country trigger this code        
    $("#cities > option").remove(); //first of all clear select items
    var salle_nom = $('#country').val();  // here we are taking country id of the selected one.
    $.ajax({
    type: "POST",
    url: "http://localhost/public_html/admin/dropdown_salle/get_cities/"+salle_nom, //here we are calling our user controller and get_cities method with the country_id
    dataType : "json",
    success: function(disciplines) //we're calling the response json array 'cities'
    {
      var discipline = new Array();

      for (i=0; i<disciplines.length; ++i) {
// remplisage des 3 tableaux :discipline, tarif, et qp_agent
      discipline[i] = disciplines[i].discipline+' pour '+disciplines[i].categorie_tarif;
      qp[i] = disciplines[i].qp_agent;
      tarif[i] = disciplines[i].tarif;
    } 

    $.each(discipline,function(id,value) //here we're doing a foeach loop round each value with id as the key and value as the value
    {
        var opt = $('<option />'); // here we're creating a new select option with for each value
         opt.val(id);
         opt.text(value);
         $('#cities').append(opt); //here we will append these new select options to a dropdown with the id 'cities'
      });

    }                  

    });

});

    function qp(qp, id) {
       return qp[id];
    }

    function tarif(tarif, id) {
    return  tarif[id];
    }

    $('#cities').change(function () {
    var a = $(this).find('option:selected').attr('value');
    //here i retrieve the qp and tarif value corresponds to my 'discipline' choosen.
    var qpValue = qp(qp, a);
    var tarifValue = tarif(tarif, a);

        var start = datestart.value;
        var end = dateend.value;
        var date1 = new Date(start);
        var date2 = new Date(end);
        var timeDiff = Math.abs(date2.getTime() - date1.getTime());
        var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
        var diffMonths = Math.round(diffDays / 30);
        alert('Vous avez Selectionnez '+diffMonths+' mois');
        var finalresult = Math.round(diffMonths*qpValue*tarifValue);

        document.getElementById("salle_montant_paye").value = finalresult;


    });

});
// ]]>
</script>



<?php $countries['#'] = 'Aucun choix'; ?>

<?php
                    foreach ($countries as $k => $v) {
                    unset ($countries[$k]);
                    $new_key1 = $v;
                    $countries[$new_key1] = $v;
                    }
?>


  <div <?php if(form_error('salle_nom')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?> >
      <label class="control-label">Nom de la Salle :</label>
      <div class="controls">
      <?php echo form_dropdown('salle_nom', $countries , $this->input->post('salle_nom') ? $this->input->post('salle_nom') : $participant_salle->salle_nom , 'id="country"'); ?>
      <span class="help-inline"><?php echo form_error('salle_nom'); ?></span>
      </div>
  </div>


<?php $cities['#'] = 'Aucun choix'; ?>
  <div <?php if(form_error('salle_discipline')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?> >
      <label class="control-label">Discipline et type:</label>
      <div class="controls">
      <?php echo form_dropdown('salle_discipline', $cities , $this->input->post('salle_discipline') ? $this->input->post('salle_discipline') : $participant_salle->salle_discipline , 'id="cities"'); ?>
      <span class="help-inline"><?php echo form_error('salle_discipline'); ?></span>
      </div>
  </div>

  <div  <?php if(form_error('salle_montant_paye')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
    <label class="control-label">Montant à payé :</label>
    <div class="controls">
    <input type="text" name="salle_montant_paye" id="salle_montant_paye" value="<?php set_value('salle_montant_paye', $participant_salle->salle_montant_paye) ?> "></input>
       <span class="help-inline"><?php echo form_error('salle_montant_paye'); ?></span>
    </div>
  </div>



  <div  <?php if(form_error('salle_versement_espec')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
    <label class="control-label">Versement espéce :</label>
    <div class="controls">
       <?php echo form_input('salle_versement_espec', set_value('salle_versement_espec', $participant_salle->salle_versement_espec)); ?>
       <span class="help-inline"><?php echo form_error('salle_versement_espec'); ?></span>
    </div>
  </div>


  <div  <?php if(form_error('salle_versement_cheq')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
    <label class="control-label">Versement chèque :</label>
    <div class="controls">
       <?php echo form_input('salle_versement_cheq', set_value('salle_versement_cheq', $participant_salle->salle_versement_cheq)); ?>
       <span class="help-inline"><?php echo form_error('salle_versement_cheq'); ?></span>
    </div>
  </div>



  <div  <?php if(form_error('salle_numero_cheq')) { echo 'class="control-group error"'; } else {echo 'class="control-group"';} ?>>
    <label class="control-label">Numéro de chèque :</label>
    <div class="controls">
       <?php echo form_input('salle_numero_cheq', set_value('salle_numero_cheq', $participant_salle->salle_numero_cheq)); ?>
       <span class="help-inline"><?php echo form_error('salle_numero_cheq'); ?></span>
    </div>
  </div>


        <div class="form-actions">
        <?php echo form_submit('submit', 'Enregistrer', 'class="btn btn-success"'); ?>
        </div>
    <?php echo form_close();?>
    </div>
    </div>
</div>

</br>

我的控制器agent.php:

public function inscriresalle ($id = NULL)  {

// Fetch a participant or set a new one
    if ($id) {
        $this->data['participant_salle'] = $this->participantsalle_m->get($id);
        count($this->data['participant_salle']) || $this->data['errors'][] = 'Agent non trouvé';

    }
    else {
        $this->data['participant_salle'] = $this->participantsalle_m->get_new();
    }
    $this->load->model('salle_m');
$this->data['countries'] = $this->salle_m->get_countries();

    // Set up the form
    $rules = $this->participantsalle_m->rules_participantsalle;
    $this->form_validation->set_rules($rules);

    // Process the form

    if ($this->form_validation->run() == TRUE) {
        $data = $this->participantsalle_m->array_from_post(array('matricule', 'nom', 'prenom', 'beneficiaire', 'sexe', 'telephone', 'date_naissance', 'date_inscription_salle', 'salle_debut_periode', 'salle_fin_periode', 'salle_montant_paye', 'salle_versement_espec', 'salle_versement_cheq', 'salle_numero_cheq'));
        $this->participantsalle_m->save($data, $id);
        redirect('admin/agent/profile/3608');
    }

    // Load the view
    $this->data['subview'] = 'admin/agent/inscriresalle';
    $this->load->view('admin/_layout_main', $this->data);
}

我的模型参与者sal_m.php:

<?php
class Participantsalle_M extends MY_Model
{

protected $_table_name = 'participants_salle';
protected $_order_by = 'id';
public $rules_participantsalle = array(

'matricule' => array(
  'field' => 'matricule', 
  'label' => 'Matricule', 
  'rules' => 'trim|required|xss_clean'
), 

'nom' => array(
  'field' => 'nom', 
  'label' => 'Nom', 
  'rules' => 'trim|required|xss_clean'
), 

'prenom' => array(
 'field' => 'prenom', 
'label' => 'Prénom', 
'rules' => 'trim|required|xss_clean'
), 

'beneficiaire' => array(
  'field' => 'beneficiaire', 
  'label' => 'Bénéficiaire', 
  'rules' => 'trim|required|xss_clean'
), 

 'sexe' => array(
   'field' => 'sexe', 
   'label' => 'Sexe', 
   'rules' => 'trim|required|xss_clean'
 ), 

'telephone' => array(
'field' => 'telephone', 
'label' => 'Téléphone', 
'rules' => 'trim|xss_clean'
), 

'date_naissance' => array(
  'field' => 'date_naissance', 
  'label' => 'Date de naissance', 
  'rules' => 'trim|required|xss_clean'
), 

'date_inscription_salle' => array(
'field' => 'date_inscription_salle', 
'label' => 'Date inscription a la salle', 
'rules' => 'trim|required|xss_clean'
), 

'salle_debut_periode' => array(
  'field' => 'salle_debut_periode', 
  'label' => 'Début période', 
  'rules' => 'trim|required|xss_clean'
), 

'salle_fin_periode' => array(
  'field' => 'salle_fin_periode', 
  'label' => 'Fin période', 
  'rules' => 'trim|required|xss_clean'
), 

'salle_nom' => array(
  'field' => 'salle_nom', 
  'label' => 'Nom de la salle', 
  'rules' => 'trim|required|xss_clean'
), 

'salle_discipline' => array(
  'field' => 'salle_discipline', 
  'label' => 'Discipline de la salle', 
  'rules' => 'trim|required|xss_clean'
), 


'salle_montant_paye' => array(
  'field' => 'salle_montant_paye', 
  'label' => 'Montant à payé', 
  'rules' => 'trim|required|xss_clean'
), 

'salle_versement_espec' => array(
'field' => 'salle_versement_espec', 
'label' => 'Versement éspece', 
'rules' => 'trim|required|xss_clean'
), 


'salle_versement_cheq' => array(
  'field' => 'salle_versement_cheq', 
  'label' => 'Versement chèque', 
  'rules' => 'trim|required|xss_clean'
), 

'salle_numero_cheq' => array(
  'field' => 'salle_numero_cheq', 
  'label' => 'Numéro de chèque', 
  'rules' => 'trim|required|xss_clean'
),


);

public function get_new()
{

$participant_salle = new stdClass();
$participant_salle->matricule = '';
$participant_salle->nom = '';
$participant_salle->prenom = '';
$participant_salle->beneficiaire = '';
$participant_salle->sexe = '';
$participant_salle->telephone = '';
$participant_salle->date_naissance = '';
$participant_salle->date_inscription_salle = '';
$participant_salle->salle_debut_periode = '';
$participant_salle->salle_fin_periode = '';
$participant_salle->salle_nom = '';
$participant_salle->salle_discipline = '';
$participant_salle->salle_montant_paye = '';
$participant_salle->salle_versement_espec = '';
$participant_salle->salle_versement_cheq = '';
$participant_salle->salle_numero_cheq = '';

return $participant_salle;
}




function __construct ()
{
parent::__construct();
}

}

一切似乎都很好,但我真的没有发现错误,为什么数据没有保存在数据库中。 我的数据库结构是:

  id
- nom
- prenom
- beneficiaire
- sexe
- telephone
- date_naissance
- date_inscription_salle
- salle_debut_periode
- salle_fin_periode
- salle_nom
- salle_discipline
- salle_montant_paye
- salle_versement_espec
- salle_versement_cheq
- salle_numero_cheq

也许这不是我编码的日子,我没有看到问题,请帮忙吗?

MY_Model上的函数save():

公共函数save($ data,$ id = NULL){

    // Set timestamps
    if ($this->_timestamps == TRUE) {
        $now = date('Y-m-d H:i:s');
        $id || $data['created'] = $now;
        $data['modified'] = $now;
    }

    // Insert
    if ($id === NULL) {
        !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
        $this->db->set($data);
        $this->db->insert($this->_table_name);
        $id = $this->db->insert_id();
            print_r($id);
    }

    // Update
    else {
        $filter = $this->_primary_filter;
        $id = $filter($id);
        $this->db->set($data);
        $this->db->where($this->_primary_key, $id);
        $this->db->update($this->_table_name);
    }

    return $id;

1 个答案:

答案 0 :(得分:0)

这条线为我打破了它。

count($this->data['participant_salle']) || $this->data['errors'][] = 'Agent non trouvé';

尝试

if (empty($this->data['participant_salle'])){
 $this->data['errors'][] = 'Agent non trouvé';
}