gpt4 book ai didi

php - 使用 codeigniter 更新

转载 作者:行者123 更新时间:2023-11-29 12:43:37 29 4
gpt4 key购买 nike

我完成了在 codeigniter user_guide 上制作新闻应用程序的教程。我现在正在尝试扩展教程网站,以便将文本加载到可以重新键入并用于更新条目的文本区域中。我希望查看文章页面是同一页面以进行更新。 View 加载正确,但是当我单击“提交”时,它会将我从 localhost/ci/news/foo 带到 localhost/ci/news/view 以及 404。我主要尝试模仿创建,并且仅更改模型查询,但由于我已经为此苦苦挣扎了一段时间,因此尝试了几种想法。

我怀疑问题可能是以下之一。

有东西引导我到 ci/news/view滥用我的 $slug 变量 Controller 和型号连接错误我的模型查询中的语法。

我将发布所有相关代码。

这是我的 View Controller 函数,这也是更新发生的地方。

public function view($slug)
{
$this->load->helper('form');
$this->load->library('form_validation');

$data['news_item'] = $this->news_model->get_news($slug);

$slug = $slug;

$this->form_validation->set_rules('text', 'text', 'required');

if (empty($data['news_item']))
{
show_404();
}

$data['title'] = $data['news_item']['title'];
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
else
{
$this->news_model->update_news();
$this->load->view('news/success');
}
}

这是模型中我的 update_news()

public function update_news($slug)
{
$data = array(
'slug' => $slug,
'text' => $this->input->post('text')
);
$this->db->set('slug', $slug);
$this->db->update('news', $data);
}

和我的 view.php

<?php 
echo validation_errors();
echo form_open('news/view');

echo '<h2>'.$news_item['title'].'</h2>';
?>

<label for="text">Text</label>
<textarea name="text"><?php echo $news_item['text']; ?></textarea><br />

<input type="submit" name="submit" value="Update" />
</form>

更新:根据要求,我的 get_news 模型函数。

public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}

$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}

最佳答案

在你的模型中只需放置 where 而不是 set。

 public function update_news($slug)
{
$data = array(
'slug' => $slug,
'text' => $this->input->post('text')
);
$this->db->where('slug', $slug);
$this->db->update('news', $data);
}

关于php - 使用 codeigniter 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25769993/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com