用户
 找回密码
 入住 CI 中国社区
搜索
查看: 3020|回复: 9
收起左侧

[已解决] 新人,数据库更新操作问题?

[复制链接]
发表于 2009-12-7 16:31:08 | 显示全部楼层 |阅读模式
php初学者,刚接触CI没多久,跟着那个20分钟视频学习一下,加了一个添加,修改等动作。在添加修改文章的时候,出现了问题。
控制器代码:
<?php
class Welcome extends Controller {
function Welcome()
{
  parent::Controller();
  
  $this->load->helper('url');
  $this->load->helper('form');
  $this->load->scaffolding('entries');
}

function index()
{
  $data['title'] = "我的博客标题";
  $data['heading'] = "我的博客头部";
  $this->db->orderby("id", "desc");
  $data['query'] = $this->db->get('entries');
  
  $this->load->view('welcome_message',$data);
}

function comments()
{
  $data['title'] = "我的评论";
  $data['heading'] = "查看评论";
  $this->db->where('entry_id',$this->uri->segment(3));
  $data['query'] = $this->db->get('comments');
  
  $this->load->view('comment_view',$data);
}

function comments_insert()
{
  $this->db->insert('comments',$_POST);
  
  redirect('welcome/comments/'.$_POST['entry_id']);
}

function article()
{
  $data['title'] = "我的日志";
  $data['heading'] = "添加文章";
  $this->load->view('article',$data);
}

function article_add()
{
  $this->db->insert('entries',$_POST);
  
  redirect('welcome');
}

function article_del()
{
  $this->db->where('id',$this->uri->segment(3));
  $this->db->delete('entries');
  
  redirect('welcome');
}

function update()
{
  $data['title'] = "我的文章";
  $data['heading'] = "修改文章";
  $this->db->where('id',$this->uri->segment(3));
  $data['query'] = $this->db->get('entries');
  
  $this->load->view('update',$data);
}

function  {
  $this->db->where('id',$this->uri->segment(3));
  $this->db->update('entries',$_POST);
  
  redirect('welcome');
}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */
?>

其它方法都可以,就是article_up()不能实现。。。。。把$this->uri->segment(3)换成ID数字又可以。。。但同样方法在删除操作里又可以。
不知道为什么?

刚学没多久,大家不要见笑

发表于 2009-12-7 18:09:46 | 显示全部楼层
具体是报什么错误呢?
 楼主| 发表于 2009-12-7 19:12:14 | 显示全部楼层
没有报错,数据库也没更新。。。。
我怀疑这个代码有问题
$this->uri->segment(3)
直接改成ID号:比如说7,然后他就成功。。。。
发表于 2009-12-7 19:17:54 | 显示全部楼层
你的 URL 是什么样子的?第三段 URL 是什么内容?
 楼主| 发表于 2009-12-7 19:24:05 | 显示全部楼层
最后一个方法function article_up()

{

$this->db->where('id',$this->uri->segment(3));

$this->db->update('entries',$_POST);



redirect('welcome');

}

view:
<?php $row = $query->row_array();?>

<p><?=anchor('welcome/','返回');?></p>
<?=form_open('welcome/article_up');?>

<p><textarea name="body" rows="10"><?=$row['body']?></textarea></p>
<p><input name="title" type="text" value="<?=$row['title']?>"></p>
<p><input type="submit" value="确定"></p>




ab.jpg
修改后提交,没有改变
 楼主| 发表于 2009-12-7 19:24:47 | 显示全部楼层
 楼主| 发表于 2009-12-7 19:31:18 | 显示全部楼层
是文章自增ID号
发表于 2009-12-7 22:56:14 | 显示全部楼层
看起来没什么问题呀,没报任何错误?
 楼主| 发表于 2009-12-11 15:26:27 | 显示全部楼层
本帖最后由 lason 于 2009-12-11 15:31 编辑

function article_up()
{
$data=array(
          'title'=>$_POST['title'],
          'body'=>$_POST['body'],
         );
$this->db->update('entries',$data,array('id' => $_POST['id']));
echo '成功';
}
改了一下,可以了,,,,,,
用这句不行,不知道为什么
$this->db->where('id',$this->uri->segment(3));
 楼主| 发表于 2009-12-11 15:42:24 | 显示全部楼层
$this->db->update('entries',$data,array('id' => $_POST['id']));
update有第三个参数没想到,手册里没有提到

本版积分规则