使用Codeigniter从数据库中删除数据

时间:2015-05-26 13:54:28

标签: php mysql codeigniter

我试图这样做,当我按下一个按钮时,它会删除数据库中的数据,但它会一直收到此错误

Message: Undefined property: stdClass::$student_id

Filename: views/delete_view.php

查看 - delete_view.php

<html>
<head>
    <title>Delete Data From Database Using CodeIgniter</title>

    <!-------------Importing google fonts------------->
    <link href='http://fonts.googleapis.com/css?family=Marcellus' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url()?>./css/delete.css">
</head>
<body>
    <div id="container">
        <div id="wrapper">
            <h1>Delete Data From Database Using CodeIgniter </h1><hr/> 
            <div id="menu">
                <p>Click On Menu</p>
                 <!--------Displaying Fetched Names from database in Links----------> 
                <ol>
                    <?php foreach ($students as $student): ?>
                        <li><a href="<?php echo base_url() . "delete_ctrl/show_student_id/" . $student->student_id; ?>"><?php echo $student->student_name; ?></a></li>
                    <?php endforeach; ?>
                </ol>
            </div>
            <div id="detail">

               <!--------Displaying Fetched Details from database ---------->   
                <?php foreach ($single_student as $student): ?>
                    <p>Student Detail</p>
                    <?php echo $student->student_name; ?><br/>
                    <?php echo $student->student_email; ?><br/>
                    <?php echo $student->student_mobile; ?><br/>
                    <?php echo $student->student_address; ?><br/>

                <!--------Delete button ---------->     
                <a href="<?php echo base_url() . "delete_ctrl/delete_student_id/" . $student->student_id; ?>"><button>Delete</button></a>
                <?php endforeach; ?>


            </div> 
        </div> 

    </div>
</body>

控制器 - delete_ctrl.php

<?php
class delete_ctrl extends CI_Controller{

function __construct(){
parent::__construct();
$this->load->model('delete_model');
}

function testing() {
    $this->load->view('delete_view');
}

//function to fetch selected record from database
function show_student_id() {
    $id = $this->uri->segment(3);
    $data['students'] = $this->delete_model->show_students();
    $data['single_student'] = $this->delete_model->show_student_id($id);
    $this->load->view('delete_view', $data);

}

//function to Delete selected record from database
function delete_student_id() {
    $id = $this->uri->segment(3);
    $this->delete_model->delete_student_id($id);
     $this->show_student_id();
}

}
?>

model - delete_model.php

 <?php
 class delete_model extends CI_Model{
//function to select all from table name students
function show_students(){
$query = $this->db->get('students');
$query_result = $query->result();
return $query_result;
}
//function to select particular record from table name students 
function show_student_id($data){
    $this->db->select('*');
    $this->db->from('students');
    $this->db->where('student_id', $data);
    $query = $this->db->get();
    $result = $query->result();
    return $result;  
  }
//function to Delete selected record from table name students   
function delete_student_id($id){
    $this->db->where('student_id', $id);
    $this->db->delete('students');   
  }
 }
 ?>

sql数据库 - 没有数据

 CREATE DATABASE college;
 CREATE TABLE students(
 Student_id int(10) NOT NULL AUTO_INCREMENT,
 Student_Name varchar(255) NOT NULL,
 Student_Email varchar(255) NOT NULL,
 Student_Mobile varchar(255) NOT NULL,
 Student_Address varchar(255) NOT NULL,
 PRIMARY KEY (Student_id)
 )

0 个答案:

没有答案