CI不会将数据添加到数据库中,而是添加' 0'

时间:2014-06-30 20:24:10

标签: database model-view-controller model continuous-integration codeigniter-2

我有一个型号,控制器&查看但是当我填写表单时,它只是将数据添加到数据库中,其中所有值都为空,任何人都可以帮忙吗?我已经尝试了一切,我无法让它工作,我有与注册部分相同的问题,但是只是不会在数据库中添加任何东西,我已经在配置中将它连接到它所以我知道它不是那样的。

控制器

<?php class Job extends CI_Controller { 

public function details() 
 { 
 $this->load->model('JobModel'); 
 $data['msg']=$this->JobModel->getJob(); 
 $this->load->view("details",$data); 
 } 


public function addJobForm(){ 
$this->load->helper('form'); 
$data['title']="Add a New Job"; 
$this->load->view("new_job_form",$data); 
} 

public function NewJob(){ 
$this->load->helper('form'); 
$data['title']="Add a New Job"; 
//$this->load->view("new_job_form",$data); 

$JobTitle=$this->input->post('JobTitle'); 
$JobDescription=$this->input->post('JobDescription'); 
$Date=$this->input->post('Date'); 
$Pay=$this->input->post('Pay'); 
$this->load->model("newjob");


if($this->newjob->addJobForm($JobTitle, $JobDescription, $Date, $Pay)){ 
 $data['msg']="Success"; 
 }

 else{ 
 $data['msg']="Something went wrong"; 
 } 
 $this->load->view("new_job_form",$data); 
} 

}

?>

模型

    <?php

Class NewJob extends CI_Model
{

  function JobModel(){
    parent::Model();
    $this->load->helper('form');                
  }

  function AddJobForm(){
    $this->load->database();
    $data = array(
              'JobTitle'=>$this->input->post('JobTitle'),
              'JobDescription'=>$this->input->post('JobDescription'),
              'Date'=>$this->input->post('Date'),
              'Pay'=>$this->input->post('Pay'),
              );
    $this->db->insert('jobs',$data);
  }
}
?>

HTML表单

<!DOCTYPE HTML> 
<html> 
<head> 
 <title><?php echo $title;?></title>
 <link rel="stylesheet" href="../CSS/style.css">
 <meta http-equiv="content-type" content="text/html;charset=utf-8" /> 
</head> 
<body>
 <? php include('navigationbar.php')?>
 <div id="container">
 <form style="text-align: center;width: 600px;
    margin-left: auto;
    margin-right: auto;
    background-color: #ffd105;
    padding: 5px;
    display: block;
    color: #fff;
    text-decoration: none;">
   <?php echo form_open('job/newjob'); ?>
   <label for="JobTitle">Job Title</label>
     <input type="text" size="20" id="JobTitle" name="JobTitle"/>
     <br/>
     <label for="JobDescription">Job Description:</label>
     <input type="text" size="20" id="JobDescription" name="JobDescription"/>
     <br/>
     <label for="Date">Date:</label>
     <input type="text" size="20" id="Date" name="Date"/>
     <br/>
     <label for="Pay">Pay:</label>
     <input type="text" size="20" id="Pay" name="Pay"/>
     <br/>
     <input type="submit" value="Add Job"/>
   </form>
 <a href="http://helios.hud.ac.uk/u1059010/assignment2/CI/index.php/home/home_view/">Home</a>
 </div>
</body> 
</html>

2 个答案:

答案 0 :(得分:0)

您没有将任何数据从控制器传递到您的模型更好地自动加载您的模型,并且不要忘记mvc,您的模型要查看控制器以进行建模。

 <?php

    Class NewJob extends CI_Model
    {

      function JobModel(){
        parent::Model();
        $this->load->helper('form');                
      }

      function AddJobForm($JobTitle, $JobDescription, $Date, $Pay){

        $data = array(
                  'JobTitle'=>$JobTitle,
                  'JobDescription'=>$JobDescription,
                  'Date'=>$Date,
                  'Pay'=>$Pay
                  );
        $this->db->insert('jobs',$data);
      }
    }
    ?>

答案 1 :(得分:0)

视图文件中的问题  删除

<form style="text-align: center;width: 600px;
margin-left: auto;
margin-right: auto;
background-color: #ffd105;
padding: 5px;
display: block;
color: #fff;
text-decoration: none;">

</form>

来自您的代码 您已使用html标签创建表单,并使用codeigniter函数再次创建表单。 你可以得到帮助 http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html