如何更新和删除表中的数据?

时间:2014-11-12 11:52:48

标签: php codeigniter crud

我是codeigniter的新手,我想更新和删除表中的数据。有人可以帮忙解决这个问题吗?我尝试了其他选择,但没有成功。

以下是我的观点:

    <script>
        function show_confirm(act) {
            if (act == "edit")
                var r = confirm("Do you really want to edit this?");

            else 
                var r = confirm("Do you really want to delete this?");

            if (r==true){
                window.location="<?php echo base_url(); ?>site/"+act;
            }
        }
    </script>
    <style>

        select{
            position: absolute;
            left: 35%;
            height: 5%;
            width: 33%;
            border-radius: 5px;
            font-family: arial;
        }

        table {
            background: #333;
            width: 700px;
            border-collapse: 1px;
            font-family: arial;
            position: absolute;
            top: 33%;
            left: 25%;
            color: #777;
        }

        th {
            padding-top: 10px;
            padding-bottom: 10px;
        }

        td {
            background: #999;
            padding: 10px;
            text-align: center;
            font-size: 12px;
        }

        .data {
            color: #555;
        }

        .data:hover {
            background: #FFF;
        }

        #action {
            font-weight: bold;
            color: #444;
            text-decoration: none;
        }

        #action:hover {
            color: #FFF;
        }

    </style>
</head>

<body>
    <header>
        <div class="menu">
            <h1 class="sis">SIS</h1>
            <p class="welcome">Welcome!&nbsp &nbsp| &nbsp</p>
            <p class="logout"><a href="logout.php">Logout</a></p>
            <nav class="navi">
              <ul>
                  <li><a href="home">Home</a></li>
                  <li><a href="#">About</a>
                        <ul class="about">
                            <li><a href="vismis">Vision/Mission</a></li>
                            <li><a href="history">History</a></li>
                        </ul>
                  </li>
                  <li><a href="admission">Admission</a></li>
                  <li><a href="calendar">Calendar</a></li>
                  <li><a href="#">Students</a>
                        <ul class="stud">
                            <li><a href="gradeI">Grade I</a></li>
                            <li><a href="gradeII">Grade II</a></li>
                            <li><a href="gradeIII">Grade III</a></li>
                            <li><a href="gradeIV">Grade IV</a></li>
                            <li><a href="gradeV">Grade V</a></li>
                            <li><a href="gradeVI">Grade VI</a></li>
                            <li><a href="acadrecs">Academic Records</a></li>
                        </ul>
                  </li>
                  <li><a href="#">Teachers</a>
                        <ul class="teach">
                            <li><a href="#">Gradesheet</a></li>
                            <li><a href="#">Lesson Plan</a></li>
                        </ul>
                  </li>
                  <li><a href="contact">Contact Us</a></li>
              </ul>
            </nav>
        </div>
    </header>
    <header>
      <div class="container">

            <select>
                <option placeholder="section"></option>
                <option value="section">Section I</option>
                <option value="section">Section II</option>
                <option value="section">Section III</option>
                <option value="section">Section IV</option>
                <option value="section">Section V</option>
                <option value="section">Section VI</option>
            </select>

            <table>
                <tr id="field">
                    <th scope="col"> ID </th>
                    <th scope="col"> First Name </th>
                    <th scope="col"> Last Name </th>
                    <th scope="col"> Contact # </th>
                    <th scope="col"> Address </th>
                    <th scope="col" colspan="2"> Action </th>
                </tr>

                <?php foreach ($user_list as $u_key) { ?>

                <tr class="data" >
                    <td><?php echo $u_key->ID; ?></td>
                    <td><?php echo $u_key->FNAME; ?></td>
                    <td><?php echo $u_key->LNAME; ?></td>
                    <td><?php echo $u_key->CONTACT; ?></td>
                    <td><?php echo $u_key->ADDRESS; ?></td>
                    <td width="40" align="left"><a id="action" href="#" onClick="show_confirm('edit',<?php echo $u_key->ID; ?>)">Edit</a></td>
                    <td width="40" align="left"><a id="action" href="#" onClick="show_confirm('delete',<?php echo $u_key->ID; ?>)">Delete</a></td>
                </tr>

                <?php } ?>

            </table>

      </div>
    </header>
</body>
</html>

我的控制器:

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

class Site extends CI_Controller {
public function index(){
    $this->home();
    $this->load->helper('url');
}

public function home(){
    $data['title'] = "Home";
    $this->load->view("view_main");
}

function vismis(){
    $data['title'] = "Vismis";
    $this->load->view("view_vismis");
}

function history(){
    $data['title'] = "History";
    $this->load->view("view_history");
}

function admission(){
    $data['title'] = "Admission";
    $this->load->view("view_admission");
}

function contact(){
    $data['title'] = "Contact";
    $this->load->view("view_contact");
}

function calendar($year = null, $month = null){

    $data['title'] = "Calendar";

    if(!$year) {
        $year = date('Y');
    }

    if(!$month) {
        $month = date('m');
    }

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

    if ($day = $this->input->post('day')) {
        $this->cal_model->add_cal_data(
            "$year-$month-$day", 
            $this->input->post('data')
            );
    }

    $data['calendar'] = $this->cal_model->generate($year, $month);

    $this->load->view("view_calendar",$data);

}

public function gradeI(){

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

    $data['title'] = "gradeI";

    $data['user_list'] = $this->stud_model->get_users();

    $this->load->view('view_students', $data);

}

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

    $udata['fname'] = $this->input->post('fname');
    $udata['lname'] = $this->input->post('lname');
    $udata['contact'] = $this->input->post('contact');
    $udata['address'] = $this->input->post('address');

    $res = $this->stud_model->insert_user_to_db($udata);

    if ($res){
        header('location:'.base_url()."site/gradeI");
    }
}

public function edit() {

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

    $data['title'] = "edit";

    $id = $this->uri->segment(3);

    $data['user'] = $this->stud_model->getById($id);

    $this->load->view('view_editstud', $data);

}

public function update_stud() {

    $mdata['fname'] = $_POST['fname'];
    $mdata['lname'] = $_POST['lname'];
    $mdata['contact'] = $_POST['contact'];
    $mdata['address'] = $_POST['address'];

    $res = $this->stud_model->update_info($mdata, $_POST['id']);

    if($res) {
        header('location'.base_url()."site/gradeI");
    }

}

public function delete() {
    $this->load->model('stud_model');

    $id = $this->uri->segment(3);

    $res = $this->stud_model->del_stud($id);

    if ($res) {
        header('location:'.base_url()."site/gradeI");
    }
}

function gradeII(){

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

    $data['title'] = "gradeII";

    $data['user_list'] = $this->stud_model->get_users();

    $this->load->view('view_stud_two',$data);
}

function gradeIII(){
    $data['title'] = "gradeIII";
    $this->load->view('view_students');
}

function gradeIV(){
    $data['title'] = "gradeIV";
    $this->load->view('view_students');
}

function gradeV(){
    $data['title'] = "gradeV";
    $this->load->view('view_students');
}

function gradeVI(){
    $data['title'] = "gradeVI";
    $this->load->view('view_students');
}

function acadrecs(){
    $data['title'] = "acadrecs";
    $this->load->view('view_acadrecs');
}

public function enroll(){
    $data['title'] = "enroll";
    $this->load->view('view_enroll');
}

}

和我的模特:

<?php

class Stud_model extends CI_Model
{


function __construct()
{
    parent::__construct();
    $this->load->database("sis");
}

public function get_users() {

    $query = $this->db->get('students');

    return $query->result();

}

public function insert_user_to_db($udata) {

    return $this->db->insert('students', $udata);

}

public function getById($id) {
    $query = $this->db->get_where('students', array('id'=>$id));
    return $query->row_array();
}

public function update_info($data, $id) {

    $this->db->where('ID', $id);
    return $this->db->update('students', $data);

}

public function del_stud($id) {

    $this->db->where('ID', $id);
    return $this->db->delete('students');

}
}

点击删除时没有任何反应。

在编辑链接上......

观点:

<form method="post" action="<?php echo base_url();?>site/update_stud">

    <?php extract($user); ?>

    <table>

        <tr>
            <th scope="row">Enter your first name</th>
            <td><input type="text" name="fname" size="20" value="<?php echo $fname; ?>" /></td>
        </tr>

        <tr>
            <th scope="row">Enter your last name</th>
            <td><input type="text" name="lname" size="20" value="<?php echo $lname; ?>" /></td>
        </tr>

        <tr>
            <th scope="row">Enter your contact number</th>
            <td><input type="text" name="contact" size="20" value="<?php echo $contact; ?>" /></td>
        </tr>

        <tr>
            <th scope="row">Enter your address</th>
            <td><textarea name="address" rows="5" cols="20"><?php echo $address; ?>      </textarea></td>
        </tr>

        <tr>

            <td><input type="hidden" name="id" value="<?php echo $id; ?>" />
                <input type="submit" name="submit" value="Update" /></td>
        </tr>

    </table>

此错误将显示:

  

输入您的名字遇到PHP错误

     

严重性:注意

     

消息:未定义的变量:fname

     

文件名:views / view_editstud.php

     

行号:60

     

“/&gt;

以及其他输入类型。

1 个答案:

答案 0 :(得分:0)

首先,javascript确认链接都不起作用,因为你实际上没有通过act传递id:

<script>
    function show_confirm(act, id) {

        if (act == "edit") {
            var r = confirm("Do you really want to edit this?");
        } else {
            var r = confirm("Do you really want to delete this?");
        }

        //Just FYI, the above can be written like this:
        //var r = (act == 'edit') ? confirm("Do you really want to edit this?") : confirm("Do you really want to delete this?");
        //or even
        //var r = confirm("Do you really want to "+act+" this?");

        if (r == true) {
            window.location = "<?php echo base_url(); ?>site/" + act + "/" + id;
        }

        //Another FYI, the above could be written like this:
        // !r || window.location = "<?php echo base_url(); ?>site/" + act + "/" + id;
    }
</script>

显然,您可以删除评论。

其次,您不需要使用$this->uri->segment()将params传递给控制器​​方法,因为codeigniter会自动传递它们(除非您使用URI ROUTING),您可以改为:

public function delete($id)
{
    $this->load->model('stud_model');

    $res = $this->stud_model->del_stud($id);

    //There is no point in this if statement as it will just display a 
    //blank page if $res evaluates to FALSE
    if ($res) {
        //As you aren't using a variable below you might as well use single quotes
        //Also, instead of using header('Location') you should use redirect() with codeiginiter
        redirect('site/gradeI');
    }
}

最后,您应该考虑验证,例如codeigniters内​​置于Form_validation库。

希望这有帮助!