2010-06-23 7 views
3

HIInsérer dans la base de données en utilisant CodeIgniter

je veux insérer des données en utilisant le formulaire en CI mais je fais face au problème Voici mon code modèle

function add_form() { 
    $this->load->database(); 
    $id = $this->input->post('id'); 
    $name = $this->input->post('name'); 
    $age = $this->input->post('age');  
    $data = array(
     'name' => $this->input->post('name'), 
     'age' => $this->input->post('age'), 
    ); 
    $this->db->insert('user',$data); 
} 

Voici mon code de contrôleur

function simpleform() { 
    $this->load->helper('form'); 
    $this->load->helper('html'); 
    $this->load->model('welcomedb_model'); 
    if($this->input->post('submit')) { 
     $this->welcomedb_model->add_form(); 
    } 
    $this->load->view('welcomedb_view'); 
} 

et voici mon code de vue

<?php echo form_open('welcomedb/submit'); ?> 
    <? echo $name; ?>: 
    <? echo form_input('name'); ?> 
    </br> 
    <? echo $age; ?>: 
    <? echo form_input('age'); ?> 
    </br> 
    <?php echo form_submit('submit', 'Submit'); ?> 
    <?php echo form_close(); ?> 

Merci pour votre aide

+0

Code de vue: :
:
webkul

+1

Veuillez rééditer votre question et formater le code en conséquence. Vous étiez proche, mais manqué au premier essai. En outre, votre code d'affichage devrait être là, pas dans un commentaire. – MJB

+0

Veuillez reformater votre code. – ggfan

Répondre

10

Votre formulaire soumet à welcomedb/submit, mais votre contrôleur semble être à welcomedb/simpleform ... vous devez peut-être changer.

Sinon, il ne semble pas y avoir de problème.

+0

je l'ai fait, mais son ne fonctionne pas – webkul

+0

Enfin, j'ai trouvé la solution, il devrait être Merci pour l'aide – webkul

+3

Montrer au gars un peu de crédit .. vérifier sa réponse – mosid

2

Probablement:

$data = array(
    'name' => $this->input->post('name'), 
    'age' => $this->input->post('age'), 
); 

Supprimer comma du dernier élément (âge) comme ceci:

$data = array(
    'name' => $this->input->post('name'), 
    'age' => $this->input->post('age') 
); 

et toujours de vidage erreur.

+0

C'est PHP pas JS !! Il est généralement recommandé d'ajouter une virgule au dernier élément du tableau. –

1
Best way is to do these code ...... 
First conifg the autoload and database. 
into autoload:$autoload['libraries'] = array('database'); 
       $autoload['helper'] = array('url','form','file'); 
Into controller 

<?php 
    class CDemo extends CI_Controller 
    { 

     function index() 
     { 
      $this->load->view('VDemo'); 
     } 
     function save() 
     { 
      $this->load->model('MDemo'); 

      if($this->input->post('submit')) 
      { 
       $this->MDemo->process();     
      } 
      redirect('CDemo'); 
     } 
    } 
?> 

Into Model 
<?php 
    class MDemo extends CI_Model 
    { 
     function process() 
     { 
      $Id = $this->input->post('Id'); 
      $Name = $this->input->post('Name'); 
      $data = array(
        'Id'=>$Id, 
        'Name'=>$Name      
        ); 
        $this->db->insert('test',$data);  
      } 
     } 
?> 

Into View 
<html> 
<head> 
<title>DEMO</title> 
</head> 
<body> 
    <h1>Form Biodata</h1> 
    <?php 
     echo form_open('CDemo/save', array('name' => 'VDemo'));  
    ?> 
     <table> 
      <tr> 
       <td>Id :</td> 
       <td><input type="text" name="Id"></input></td> 
      </tr> 
      <tr> 
       <td>Name :</td> 
       <td><input type="text" name="Name"></input></td> 
      </tr>  
      <tr> 
       <td><input type="submit" name="submit" value="submit"></input></td> 
      </tr>   
     </table> 
    <?php 
     echo form_close(); 
    ?> 
    <textarea rows="" cols="">Hello</textarea> 
</body> 
</html> 
Questions connexes