donglu4633 2011-11-21 03:40
浏览 25

使用CodeIgniter(MVC)将表单连接到数据库

My assignment is to use CodeIgniter (an MVC framework) and connect the form that's in the view to the database. I have done most of the code, and I believe most of it is correct. I finally got the view page (form) to load, but I cannot get the Register function in the controller to load. Does anyone see any errors in my code? It would help me a great deal.

form.php (view):

      <!DOCTYPE html>
 <html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title><?php //echo $title;?></title>
</head>
<body>

    <h1>Register for a Dorm</h1>
    <form method="post"        
    action="http://ispace.ci.fsu.edu/~trm07/assignment4/index.php/controller/register/">

        <fieldset>

            <label>First Name</label>
            <input type="text" name="first_name"/>

            <label>Last Name</label>
            <input type="text" name="last_name"/>
            </fieldset>

        <label>Undergraduate Level:</label>

        <?php

        //dropdown list for level
    echo '<select name="level">';
    $level = array('Freshman','Sophomore', 'Junior', 'Senior');
        foreach ($level as $selection) {
        echo '<option value="'.$selection.'">'.$selection.'</option>';
    }
echo '</select>';
    ?>

        <label>Gender:</label>

        <?php

        //dropdown list for gender
        echo '<select name="gender">';
    $gender = array('Male','Female');
        foreach ($gender as $gend_selection) {
        echo '<option
   value="'.$gend_selection.'">'.$gend_selection.'</option>';
    }
echo '</select>';


        //radio buttons for dorms



        $requested_dorm = array('Landis','Salley','DeGraff','Cawthon','Reynolds');

        echo("<fieldset><legend>Requested Dorm</legend>");

        foreach ($requested_dorm as $dorm_names){
            echo "<input type='radio' name='dorm_name' value='$dorm_names' /> 
     $dorm_names <br />";

        }

         ?>

        <br><input type="submit" value="Register">


    </body>
 </html>



                 controller.php


 <?php 

 class Controller extends CI_Controller {

 function index()
 {   //$data['title'] = "Register for a Dorm";
    $this->load->view('form');
 }

  function show()
 {
    $this->load->model('model');

    $dorms = $this->model->get_dorms();

    foreach($dorms as $dorm){
        if($dorm['dorm_name'] == $dorm_name)
            $chosen_dorm = $dorm['dorm_name'];
    }

  }


   function register()
  {
   $this->load->library('form_validation');           

   $view_data = array('message' => '');

   //If the form was submitted, process it
   if (count($_POST) > 0)
    { $dorm_name = $this->input->post('dorm_name');
      $first_name = $this->input->post('first_name');
      $last_name = $this->input->post('last_name');
      $level = $this->input->post('level');
      $gender = $this->input->post('gender');

    {          
      //Validate the input

      $this->form_validation->set_rules('first_name', 'First Name',
   'required|strip_tags|trim');
      $this->form_validation->set_rules('last_name', 'Last Name',  
   'required|strip_tags|trim');

      $val_result = $this->form_validation->run();

      //Add the data to the database
      if ($val_result == TRUE)
      {
        $this->load->model('model');
        $db_result = $this->model->add_student_to_dorm($_POST['dorm_name'],  
  $_POST['first_name'], $_POST['last_name'], $_POST['level'], $_POST['gender']);

        if ($db_result == TRUE)
        {
          $view_data['message'] = "Added student to the dorm!";  
          //$view_data['message'] = "Added" . "$_POST['first_name']" . " " . 
 "$_POST['last_name']". " to " . "$_POST['dorm_name']" .  " hall.";
        }
        else
        {
          $view_data['message'] = "An error occured adding the student to the dorm!";
        }
      }
      }



      $this->load->view('form',$view_data);
  //$this->load->model('model');
  //$this->model->get_students();
   }
  } 
 }
 ?>

model.php:

 <?php

  class Model extends CI_Model {

   //function to take student info posted from form, and adds to database

   public function add_student_to_dorm()
   {
    $this->load->database($dorm_name, $first_name, $last_name, $level, $gender);

    $data = array
    (
        'dorm_name' =>$dorm_name,
        'student_fname' => $first_name,
        'student_lname' => $last_name,
        'student_level' => $level,
        'student_gender' => $gender,

    );

    $result = $this->db->insert('student', $data);

    return $result;
    }

     //get dorm table results in an array from database, return its rows

      public function get_dorms()
   {
   $this->load->database();     
   $dorms = $this->db->get('dorm');
   $rows = $dorms->result_array();
   return $rows;

    }

   //get student table results in an array from database, return its rows
    public function get_students()
   {
    $this->load->database();     
    $students = $this->db->get('student');
    $stu_rows = $students->result_array();
    return $stu_rows;

    }

    }

   ?>
  • 写回答

1条回答 默认 最新

  • dp20011 2011-11-21 04:53
    关注

    Here you called add_student_to_dorm() function in the controller page with parameters but in model you doesnot take that parameters that is

    $db_result = $this->newmodel->add_student_to_dorm($_POST['dorm_name'],  
          $_POST['first_name'], $_POST['last_name'], $_POST['level'], $_POST['gender']);
    

    this is the function in controller but your model is only have public function add_student_to_dorm() no variables So change public function add_student_to_dorm() to

    public function add_student_to_dorm($dorm_name, $first_name, $last_name, $level, $gender)
    

    and also change

    $this->load->database($dorm_name, $first_name, $last_name, $level, $gender); to

    $this->load->database(); in model then it is working

    评论

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败