我的输入不会在codeigniter中插入我的数据库

时间:2016-02-18 07:49:00

标签: php mysql

我正在尝试使用CI构建一个todo应用, 我将输入数据插入数据库时​​遇到问题。 这是我的代码。

的index.php

 <?php echo form_open('Welcome/Add_task');?>
    <form class="col-md-8" role="form" method="POST">

        <span class="form-group">
            <label class="control-label">Name Of The Task:</label>
            <input type="text" name="task" class="form-control" ng-model="todo.task"></input>
        </span>
        <span class="form-group">
            <label class="control-label">status:</label>
            <input type="task" name="status" class="form-control" ng-model="todo.statuss"></input>
        </span>
        <span class="form-group">
            <label class="control-label">Task Description:</label>
            <textarea name="description" class="form-control" ng-model="todo.descriptions"></textarea>
        </span>
        <span class="form-group">
            <label class="control-label">Due Date:</label>
            <input type="date" name="date" class="form-control" ng-model="todo.date"></input>
        </span><br>

        <input type="submit" class="btn btn-info btn-block form-control" name="add" value="Create Task"></input>
       <!-- <?php echo form_close(); ?>-->
    </form>

的welcome.php

    <?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function index()
    {
        $this->load->view('welcome_message');


    }

    public function Add_task()
    {

        $task=$this->input->POST('task');
        $status=$this->input->POST('status');
        $description=$this->input->POST('description');
        $date=$this->input->POST('date');

        $data=array('task'=>$task,'status'=>$status,'description'=>$description,'date'=>$date); 
        $this->load->model('Add_task');

        if($this->Add_task->add($data))
        {
            echo "data inserted successfully";
        }

        else
        {
            echo "data not inserted";
        }

    }

}

_Add_task.php _

    <?php
class Add_task extends CI_Controller {

    public function add($data)
    {
        $this->load->database();

        $count = $this->db->insert('user',$data);
        if($count>0)
        {
            echo "true";
        }
        else
        {
            echo "false";
        }
    }
}

?>

我在这里得到的错误是,当我单击提交按钮时,Add_task.php中的if条件返回true,但welcome.php中的if条件返回false。有人提出了一些纠正措施来解决这个问题。

1 个答案:

答案 0 :(得分:0)

     1.remove this line

        <?php echo form_open('Welcome/Add_task'); ?>


    2.and edit the form tag also

        <form class="col-md-8" role="form" method="post" 
        action="<?php echo base_url(); ?>Welcome/Add_task" >

    3.<input type="text" name="status" class="form-control" ng-model="todo.statuss"> 

    4.  $task=$this->input->post('task'); 
//POST use smaller case all input->post

    5.  //model edit
    class Add_task extends CI_Model {

    public function add($data)
    {
    $this->load->database();

    $this->db->insert('user',$data);
    return $num_inserts = $this->db->affected_rows();
    }
    }
    6. //controller edit

    $rows=$this->Add_task->add($data);
    if($rows>0)
    {
    echo "data inserted successfully";
    }

    else
    {
    echo "data not inserted";
    }