Php 如何更新和删除表中的数据?
我是codeigniter的新手,我想从表中更新和删除数据。有人能帮我解决这个问题吗?我尝试过其他方法,但没有成功 以下是我的看法:Php 如何更新和删除表中的数据?,php,codeigniter,crud,Php,Codeigniter,Crud,我是codeigniter的新手,我想从表中更新和删除数据。有人能帮我解决这个问题吗?我尝试过其他方法,但没有成功 以下是我的看法: <script> function show_confirm(act) { if (act == "edit") var r = confirm("Do you really want to edit this?"); else
<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_editstub.php
电话号码:60
/>
其他输入类型也是如此。首先,两个javascript确认链接都不起作用,因为您实际上没有通过act传递id: 最后,您应该真正考虑验证,例如,Form_验证库中内置的codeigniters
希望这有帮助 更具体一点。。。。你犯了什么错误?很难读懂冗长的代码。只显示未按预期工作的代码。这可能不会直接回答您的问题,但是:1我认为没有必要要求用户确认编辑,因为用户确认后,您所做的一切就是提供编辑视图,此时无需编辑。2我强烈建议不要使用一种可以通过url获取-使用帖子访问的删除方法…对此我深表歉意。当我点击删除按钮时,什么也没发生。非常感谢你的帮助,罗斯!我仍在试图理解codeigniter是如何工作的。另一件事是我的编辑链接显示页面,但其中有错误。我猜是关于变量的。你能帮我一下吗?我在帖子的底部添加了它。基本上,当你使用一个PHP不知道的变量时,这个错误会显示出来。但是,你也需要向我显示view_editstub.PHP,这样我就可以看到页面上发生了什么。首先,我个人不会使用extract,因为它被认为是一种不好的做法,并且会使调试代码变得更加困难,尤其是在更深层次上。var_dump$user;死亡在使用extract$user的行之前,查看$user中的内容是否实际存在。第三,主要检查set_value函数……您可能也喜欢HTML表库。我放置了var_dump,它会出现:objectCI_DB_mysql_result17 8{[conn_id]=>resource29类型mysql link persistent[result_id]=>resource32类型mysql result[result_array]=>array0{}[result_object]=>array0{}[自定义结果对象]=>array0{}[当前行]=>int0[num\u行]=>int1[行数据]=>NULL}
<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>
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');
}
}