使用codeigniter的CRUD操作

时间:2016-06-30 05:42:58

标签: php codeigniter

我在CODEIGNITER中完成了这个CRUD操作。但是,当我尝试运行该程序时,我得到一个空白页面。而且我无法知道错误是什么。任何帮助,将不胜感激。我附上了以下代码。

Stud_controller.php `

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

class Stud_controller extends CI_Controller {

function __construct(){

    parent:: __construct();
    $this->load->helper('url');
    $this->load->database();
}

public function index()
{
    $query = $this->db->get('stud');
    $data['records'] = $query->result();

    $this->load->helper('url');
    $this->load->view('Stud_view', $data);
}

public function add_student_view(){
    $this->load->helper('form');
    $this->load->view('Stud_add');
}

public function add_student(){

    $this->load->model('Stud_Model');

    $data = array(
        'Roll_No' => $this->input->post('Roll_No'),
        'Name' => $this->input->post('Name')
    );

    $this->Stud_Model->insert($data);

    $query = $this->db->get("stud");
    $data['records'] = $query->result();
    $this->load->view('Stud_view',$data);

}

public function update_student(){

    $this->load->model('Stud_Model');

    $data = array(

        'Roll_No' => $this->input->post('Roll_No'),
        'Name' => $this->input->post('Name')
    );

    $old_Roll_No = $this->input->post('old_Roll_No');
    $this->Stud_Model->update($data,$old_Roll_No);

    $query = $this->db->get("stud");
    $data['records'] = $query->result();
    $this->load->view('Stud_view',$data);
}

public function delete_student(){

    $this->load->model('Stud_Model');
    $Roll_No = $this->uri->segment('3');
    $this->Stud_Model->delete($Roll_No);

    $query = $this->db->get("stud");
    $data['records'] = $query->result();
    $this->load-view('Stud_view',$data);
  }
 }
?>`

Stud_Model.php

<?php

Class Stud_Model extends CI_Model {

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

    public function insert($data) {
        if($this->db->insert("stud", $data)){
            return true;
        }
    }

    public function delete($Roll_No){
        if($this->db->delete("stud", "Roll_No = " .$Roll_No)){
            return true;
        }
    }

    public function update($data,$old_Roll_No){
        $this->db->set($data);
        $this->db->where("Roll_No", $old_Roll_No);
        $this->db->update("stud", $data);
    }
}

?>

Stud_view.php

<html>
<head>
<title>Student</title>
</head>
<body>
<a href = '<?php echo base_url(); ?>
index.php/stud/add_view">Add</a>

<table border = "1">
<?php
    $i = 1;
    echo "<tr>";
    echo "<td>Sr#</td>";
    echo "<td>Roll No.</td>";
    echo "<td>Name</td>";
    echo "<td>Edit</td>";
    echo "<td>Delete</td>";
    echo "</tr>";

    foreach($records as $r){
        echo "<tr>";
        echo "<td>".$i++."</td>";
        echo "<td>".$r->Roll_No."</td>";
        echo "<td>".$r->Name."</td>";
        echo "<td><a href = '".base_url()."index.php/stud/edit/"
              .$r->Roll_No."'>Edit</a></td>"; 
        echo "<td><a href = '".base_url()."index.php/stud/delete/"
              .$r->Roll_No."'>Delete</a></td>"; 
        echo "<tr>"; 

    }
    ?>
    </table>
  </body>
  </html>

4 个答案:

答案 0 :(得分:0)

  1. 检查您是否未在config / router.php中设置默认路由器。
  2. 在相同的情况下设置文件名和类名,如果将它们设置为小写,则更好。

答案 1 :(得分:0)

Naga,你的base_url应该看起来像这个例子

base_url("blog/post/123");

另外,将index.php从URL中删除。 “.base_url()。”index.php / stud / edit /“”

删除句点和index.php。你永远不会在你的网址中使用.php

答案 2 :(得分:0)

这是我的crud master使用codeigniter

public function get_data_all($tabel)
    {
        $this->db->select('*');
        $this->db->from($tabel);
        $query = $this->db->get();
        return $query->result_object();
    }

    public function get_data_all2($tabel,$where,$value)
    {
        $this->db->select('*');
        $this->db->from($tabel);
        $this->db->where($where, $value);
        $query = $this->db->get();
        return $query->result_object();
    }

    public function data_proses_komputer($tabel)
    {
        $this->db->select('*');
        $this->db->from($tabel);
        $this->db->join('artikel', 'forward_changing.id_artikel = artikel.id_artikel');
        $this->db->join('user', 'forward_changing.username = user.username');
        $query = $this->db->get();
        return $query->result_object();
    }

    public function get_data_where($tabel,$where,$id)
    {
        $this->db->select('*');
        $this->db->from($tabel);
        $this->db->where($where, $id);
        $query = $this->db->get();
        return $query->result_object();
    }

    public function insert_data($tabel,$data)
    {
        $this->db->insert($tabel,$data);
        return TRUE;
    }

    public function delete_data($tabel,$where)
    {
        $res = $this->db->delete($tabel,$where);
        return $res;
    }

    public function do_update_data($tabel,$data_insert,$where)
    {
        $res = $this->db->update($tabel, $data_insert, $where);
        return $res;
    }

    public function tampil($table){

    return $this->db->get_where($table);
    }

答案 3 :(得分:0)

创建一个视图文件add.php

<script type="text/javascript" src="<?=$this->config->item('js_path')?>jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="<?=$this->config->item('js_path')?>jquery-validation/js/jquery.validate.min.js"></script>
<?php $formAction = !empty($editRecord) ? 'update_record' : 'insert_record';

?>
<!DOCTYPE html>
<html>
<head>
    <title>user</title>
</head>
<body>
    <form id="signup_form" method="post" action="<?= base_url('admin/user_management_control/'.$formAction) ?>" >
        <table border="1" align="center" width="450">
            <tr>
                <td>First Name</td>
                <td>
                    <input type="text" name="first_name" id="first_name" value="<?php echo !empty($editRecord[0]->first_name) ? $editRecord[0]->first_name :'' ?>">
                </td>
            </tr>

            <tr>
                <td>Last Name</td>
                <td>
                    <input type="text" name="last_name" id="last_name" value="<?php echo !empty($editRecord[0]->last_name) ? $editRecord[0]->last_name :'' ?>">
                </td>
            </tr>

            <tr>
                <td>Email</td>
                <td>
                    <input type="email" name="email" id="email" value="<?php echo !empty($editRecord[0]->email) ? $editRecord[0]->email :'' ?>">
                </td>
            </tr>

            <tr>
                <td>Mobile Number</td>
                <td>
                    <input type="text" name="mobile_no" id="mobile_no" onkeypress="return isNumber(event)" data-range='[1,9999999999]' maxlength="15" value="<?php echo !empty($editRecord[0]->mobile_no) ? $editRecord[0]->mobile_no :'' ?>">
                </td>
            </tr>

           <tr>

            <tr>
                <td>Gender</td>
                <td>
                    <select name="gender" id="gender">
                        <option value="">Select Gender</option>
                        <option value="male" <?php if(!empty($editRecord[0]->gender) && $editRecord[0]->gender == 'male') echo "selected" ;?>>male</option>
                        <option value="female" <?php if(!empty($editRecord[0]->gender) && $editRecord[0]->gender == 'female') echo "selected"; ?>>Female</option>
                    </select>
                </td>
            </tr>

            <td>Address</td>
                <td>                   
                    <textarea name="address" id="address"><?php echo !empty($editRecord[0]->address) ? $editRecord[0]->address :'' ?></textarea>
                </td>
            </tr>
            <?php if(empty($editRecord[0]->id))
            {
                ?>
            <tr>
                <td>Password</td>
                <td>
                    <input type="password" name="password" id="password" value="">
                </td>
            </tr>

            <tr>
                <td>Confirm Password</td>
                <td>
                    <input type="password" name="cpassword" id="cpassword" value="">
                </td>
            </tr>
            <?php 
            }
            ?>
            <tr collspan="2">
                <td align="center">
                    <input type="submit" name="submit" value="submit">
                </td>
            </tr>

        </table>
        <input type="hidden" name="id" id="id" value="<?= !empty($editRecord[0]->id)?$editRecord[0]->id:'';?>" >
    </form>

</body>
</html>

<script type="text/javascript">
    $("#signup_form").validate({
        onkeyup: false,
        rules:{
            first_name: {
                required: true
            },
            last_name: {
                required: true
            },
            email:{
                required: true,
                email: true
            },
            mobile_no:{
                required:true,
                minlength:10,
                maxlength:15
            },
            password:{
                required:true,
                minlength:6,
                normalizer: function(value){
                    return $.trim(value);
                },
                password_check: true
            },
            cpassword:{
                required:true,
                normalizer: function(value){
                    return $.trim(value);
                },
                equalTo: "#password",
            },
        },
        messages:{
            first_name:{
                required: "First Name cannot be blank.",
            },
            last_name:{
                required: "Last Name cannot be blank.",
            },
            email:{
                required: "Email cannot be blank.",
            },
            mobile_no:{
                required: "Mobile number cannot be blank",
                minlength: "Please enter minimum 10 digit number.",
                maxlength: "Contact Number not allow more then 15 digit."
            },
            password:{
                required: "Password cannot be blank.",
                minlength: "Password should be at least 6 character long."
            },
            cpassword:{
                required: "Confirm Password cannot be blank.",
                equalTo: "Password and Confirm Password must be same." 
            },
        },
    });

    jQuery.validator.addMethod('password_check', function(value,element)
    {
        var pattern = new RegExp(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{6,}$/);
        //
        if(pattern.test(value))
        {
            return true;
        }else{
           return false;
        }
    },"Password should be 6 character long, one special character, one uppercase, one lowercase letter and one digit.");

    function isNumber(evt)
    {
         evt = (evt) ? evt : window.event;
            var charCode = (evt.which) ? evt.which : evt.keyCode;
            if (charCode > 32 && (charCode < 48 || charCode > 57)) {
                return false;
            }
            return true;

    }
</script>

我使用jQuery验证来验证字段。

创建一个文件以显示详细信息list.php

<html>
<body>
<table border="2" align="center">

    <tr>
        <td>#</td>
        <td>Name</td>
        <td>Email</td>
        <td>Mobile No.</td>
        <td>Gender</td>
        <td>Address</td>
        <td>Action</td>

    </tr>

    <?php 
    foreach($user_details as $user)
    {
        ?>
        <tr>
            <td><?php echo $user->id;?></td>
            <td><?php echo ucwords($user->name);?></td>
            <td><?php echo $user->email;?></td>
            <td><?php echo $user->mobile_no;?></td>
            <td><?php echo ucfirst($user->gender);?></td>
            <td><?php echo $user->address;?></td>
            <td><a href="<?php echo base_url('admin/user_management_control/edit_record/')?><?php echo $user->id?>">Edit</a>
            <a href="<?php echo base_url('admin/user_management_control/delete/')?><?php echo $user->id?>">Delete</a>

            </td>

        </tr>

        <?php
    }
   ?>   
   <tr>
    <td><a href="<?php echo base_url('admin/user_management_control/add')?>">Add new</a></td>
   </tr>
   </table>
   </body>
   </html>

现在创建模型Common_model.php

<?php

Class Common_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    function encrypt_script($string)
    {
        $cryptKey  = 'qJB0rGtIn5UB1xG03efyCp';
        $qEncoded      = base64_encode( mcrypt_encrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), $string, MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ) );
           return( $qEncoded );
    }

    function decrypt_script($string)
    {
        $cryptKey  = 'qJB0rGtIn5UB1xG03efyCp';
        $qDecoded      = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), base64_decode( $q ), MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ), "\0");
           return( $qDecoded );
    }

    function insert($table,$data)
    {
        $this->db->insert($table,$data);
        return $this->db->insert_id();
    }

    function get_users()
    {
        $this->db->select('id, CONCAT(`first_name`," ", `last_name`) AS `name` ,email,mobile_no,gender,address');
        $result = $this->db->get('user_master');
        return $result->result();
    }

    public function get_user_detail($id){
        $this->db->select('*');
        $this->db->where('id',$id);
        $result = $this->db->get('user_master');
        return $result->result();
    }

    public function update($table,$data,$where)
    {
        if(!empty($where))
            $this->db->where($where);
            $this->db->update($table, $data);
    }

    public function delete($table, $where)
    {

        $this->db->where($where);
        $this->db->delete($table);
    }
}
?>

现在创建控制器user_management_control.php

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


Class user_management_control extends CI_Controller
{
    public  function __construct()
    {
        parent::__construct();
        $this->load->model('Common_model');
    }

    public function index()
    {
        $data['user_details'] = $this->Common_model->get_users();
        $this->load->view('admin/user_management/list',$data);
    }

    public function add()
    {       
        $this->load->view('admin/user_management/add');
    }

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

        if($this->input->server('REQUEST_METHOD') == 'POST')
        {
            $this->form_validation->set_rules('first_name','First Name','trim|required');
            $this->form_validation->set_rules('last_name','Last Name','trim|required');
            $this->form_validation->set_rules('email','Email','trim|required|valid_email');
            $this->form_validation->set_rules('mobile_no','Mobile Number','trim|required');
            $this->form_validation->set_rules('password','Password','trim|required|matches[cpassword]');
            $this->form_validation->set_rules('cpassword','Confirm Password','trim|required');

            if($this->form_validation->run() == FALSE)
            {
                $this->load->view('admin/user_management/add');
            }
            else
            {
                $post_array = $this->input->post();

                $data = array(
                    'first_name' => $post_array['first_name'],
                    'last_name' => $post_array['last_name'],
                    'email'      => strtolower(trim($post_array['email'])),
                    'mobile_no' => $post_array['mobile_no'],
                    'gender' => $post_array['gender'],
                    'address' => $post_array['address'],
                    'password' => $this->Common_model->encrypt_script($post_array['password']),
                    'status' => 0,
                    'inserted_date' => date('Y-m-d H:i:s')
                );

                $this->Common_model->insert('user_master',$data);
                redirect('admin/user_management_control/');
            }
        }

    }

    public function edit_record()
    {
        $id = $this->uri->segment(4);
        $result = $this->Common_model->get_user_detail($id);
        if(empty($result))
        {
            redirect('admin/user_management_control/');
        }
        $data['editRecord'] = $result;
        $this->load->view('admin/user_management/add',$data);

    }

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

        if($this->input->server('REQUEST_METHOD') == 'POST')
        {
            $this->form_validation->set_rules('first_name','First Name','trim|required');
            $this->form_validation->set_rules('last_name','Last Name','trim|required');
            $this->form_validation->set_rules('email','Email','trim|required|valid_email');
            $this->form_validation->set_rules('mobile_no','Mobile Number','trim|required');

            if($this->form_validation->run() == FALSE)
            {
                $this->load->view('admin/user_management/add');
            }
            else
            {
                $post_array = $this->input->post();
                $cdata['id'] = $post_array['id'];
                $data = array(
                    'first_name' => $post_array['first_name'],
                    'last_name' => $post_array['last_name'],
                    'email'      => strtolower(trim($post_array['email'])),
                    'mobile_no' => $post_array['mobile_no'],
                    'gender' => $post_array['gender'],
                    'address' => $post_array['address'],
                    'status' => 0,
                    'modified_date' => date('Y-m-d H:i:s')
                );

                $this->Common_model->update('user_master',$data,array('id' =>$cdata['id']));
                redirect('admin/user_management_control/');
            }
        }
    }

    public function delete()
    {
        $id = $this->uri->segment(4);

        $this->Common_model->delete('user_master',array('id'=>$id));
        redirect('admin/user_management_control/');
    }

}
?>