Я новичок в 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!   |  </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
«/>
и так далее с другими типами ввода.
Во-первых, ни одна из ссылок подтверждения javascript не будет работать, потому что вы на самом деле не передаете идентификатор через act
:
<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()
передавать параметры в метод контроллера, так как 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');
}
}
Наконец, вы должны подумать о проверке, например, встроенные кодигнитеры Form_validation
Библиотека.
Надеюсь это поможет!
Других решений пока нет …