使用codeigniter的CRUD操作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用codeigniter的CRUD操作相关的知识,希望对你有一定的参考价值。

我在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>
答案

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

base_url("blog/post/123");

另外,从URL中取出index.php。 “.base_url()。 ”的index.php /螺柱/编辑/“”

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

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

这是我的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);
    }
另一答案

创建一个视图文件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."
            },
            cpasswo

以上是关于使用codeigniter的CRUD操作的主要内容,如果未能解决你的问题,请参考以下文章

CodeIgniter 中控制器或模型中的 CRUD

CodeIgniter + Doctrine:控制器中的 CRUD?

PHP codeigniter标准crud模型

[Angularjs]asp.net mvc+angularjs+web api单页应用之CRUD操作

DataTables 函数在 Codeigniter 中不起作用

11SpringBoot-CRUD-thymeleaf公共页面元素抽取