home.php 这是我的控制器,我通过Id获得正确的URL,但无法删除记录。
<?php
class home extends CI_Controller
{
function __construct(){
parent::__construct();
$this->load->model('select');
$this->load->helper(array('form','url','html'));
$this->load->library(array('form_validation','session'));
}
public function index(){
//load the database
//$this->load->database();
//load the model
$this->load->model('select');
//load the method of model
$data['h']=$this->select->select();
//return the data in view
$this->load->view('fetch_view', $data);
}
function records(){
$ytl['dta']=$this->users->datas();
//$this->load->view('info',$ytl);
//echo "<pre>controller";print_r($ytl);
}
function primezone(){
$ytl['dta']=$this->users->datas();
echo "<pre>controller";print_r($ytl);
}
function form($id=null){
//die($id);
if($this->input->post())
{
//echo"<pre>";print_r( $this->input->post());die;
$this->form_validation->set_rules('fname','First Name','required|min_length[5]');
$this->form_validation->set_rules('lname','Last Name','required|callback_check_picture[lname]');
//$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('mobile','Mobile Number','required|callback_valid_phone_number[mobile]');
$this->form_validation->set_error_delimiters('<h1 class="error">', '</h1>');
if($this->form_validation->run()==True)
{
$data=[
'fname'=>$this->input->post('fname'),
'lname'=>$this->input->post('lname'),
'email'=>$this->input->post('email'),
'mobile'=>$this->input->post('mobile'),
'message'=>$this->input->post('message'),
];
if(empty($id))
{
$ytl=$this->select->insertdata($data);
} else {
$ytl=$this->select->updatedata($data,$id);
}
if($ytl){
$this->session->set_flashdata('message', 'Successfully Added.');
redirect('home');
}
} else {
//$this->load->view('form');
}
}
$ytl['dta']=$this->select->getDataById($id);
//echo "<pre>";print_r($ytl);die;
$this->load->view('form',$ytl);
}
public function check_picture($a){
if(!is_numeric($a))
{
return true;
}else{
$this->form_validation->set_message('check_picture', 'Please enter only char value');
return False;
}
}
function valid_phone_number($value){
$value = strlen($value);
//echo $value;die;
if ($value == 10) {
return true;
} else {
$this->form_validation->set_message('valid_phone_number', 'Mobile number not in range'); //{10}
return false;
}
}
public function delete(){
$this->load->model('select');
$id=$this->input->get('id');
//echo $id;
if($this->select->deleteuser($id))
{
$data['h']=$this->select->select();
$this->load->view('fetch_view', $data);
}
}
}
?>
fetch_view.php 这是我的观点,我通过Id获得正确的URL,但无法删除记录。
<!DOCTYPE html>
<html>
<head>
<link href="<?php echo base_url().'assets/bootstrap.min.css'; ?>" rel="stylesheet" type='text/css'>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.13/datatables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.13/datatables.min.js"></script>
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.13/css/dataTables.bootstrap.min.css" />
<script type="text/javascript" src="https://cdn.datatables.net/1.10.13/js/dataTables.bootstrap.min.js"></script>
<style>
.error{color:red}
</style>
</head>
<body>
<h1>User List</h1>
<?php if($this->session->flashdata('message')){?>
<div class="alert alert-success alert-dismissable">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Success!</strong> <?= $this->session->flashdata('message') ?>
</div>
<?php } ?>
<table border="1" class="table table-bordered table-striped table-hover" id="">
<tbody>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Mobile Number</th>
<th>Message</th>
<th>Update</th>
<th>Delete</th>
</tr>
</thead>
</tbody>
<?php
foreach ($h->result() as $row)
{ ?>
<tr>
<td><?php echo $row->fname; ?></td>
<td><?php echo $row->lname; ?></td>
<td><?php echo $row->email; ?></td>
<td><?php echo $row->mobile; ?></td>
<td><?php echo $row->message; ?></td>
<td><a href="<?= site_url('home/form').'/'.$row->id ?>">Edit</a></td>
<td><a href="<?php echo site_url('home/delete').'/'.$row->id; ?>">Delete</a></td>
</tr>
<?php } ?>
</table>
</form>
</body>
</html>
select.php 这是我的模型,我通过Id获得正确的URL,但无法删除记录。
<?php
class select extends CI_model
{
function __construct(){
// Call the Model constructor
parent::__construct();
$this->load->database();
}
function insertdata($data){
if(!empty($data['id'])){
$this->db->where('id', $data['id'])->update('student', $data);
return $data['id'];
} else {
unset($data['id']);
$this->db->insert('student', $data);
return $this->db->insert_id();
}
}
public function select(){
//data is retrive from this query
$query = $this->db->get('student');
return $query;
//return $query->row();
}
function getDataById($id)
{
$this->db->select('*');
$this->db->where('id',$id);
$this->db->from('student');
$query = $this->db->get()->row_array();
return $query;
}
function updatedata($data, $id)
{
//print_r($data);die;
$this->db->where('id', $id);
return $this->db->update('student' ,$data);
}
public function deleteuser($id)
{
//echo $id;
//die;
// $this->load->database();
$this->db->where('id', $id);
$this->db->delete('student');
return true;
}
}
?>
我知道我只需要粘贴特定的代码,但我会粘贴完整的代码。
答案 0 :(得分:3)
你调用了url delete($ id)函数但没有传递控制器中的id。更改你的删除功能。
public function delete($id='')
{
$this->load->model('select');
if(!empty($id))
{
$data['deldata']=$this->select->deleteuser($id);
$data['h']=$this->select->select();
$this->load->view('fetch_view', $data);
}
}
答案 1 :(得分:0)
我希望能帮到你
<body>
echo $msg;
.....your code....
<a href="<?php echo base_url() . "index.php/home/delete/" . $row->id; ?>">Delete</a>
home.php
public function index($msg = NULL){
$this->load->model('select');
$data = array(
'h' => $this->select->select(),
'msg' => $msg
);
$this->load->view('fetch_view', $data);
}
public function delete()
{
$this->load->model('select');
$id = $this->uri->segment(3);
//echo $id;
$result= $this->select->deleteuser($id);
if($result==TRUE)
{
$msg = '<script>alert("delete successful!!! ")</script>';
$this->index($msg);
}
}
select.php
public function select(){
//data is retrive from this query
$this->db->select('*');
$query = $this->db->get('student');
return $query;
//return $query->row();
}
public function deleteuser($id=NULL)
{
$this->db->where('id', $id);
$result= $this->db->delete('student');
return $result;
}