codeigniter不会更新数据库

时间:2014-06-15 11:22:23

标签: php forms codeigniter

我正在尝试使用表单中的数据更新数据库, 表单中包含来自数据库的数据。 但是当我点击提交时,它会给出错误。 有人可以帮忙吗?

我的控制员:

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

    $data['title'] = 'Update a product';

    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('price', 'Price', 'required');
    $this->form_validation->set_rules('description', 'Description', 'required');
    $this->form_validation->set_rules('publisher', 'Publisher', 'required');
    $this->form_validation->set_rules('engine', 'Engine', 'required');
    $this->form_validation->set_rules('release_date', 'Release date', 'required');
    $this->form_validation->set_rules('platform', 'Platform', 'required');
    $this->form_validation->set_rules('genre', 'Genre', 'required');
    $this->form_validation->set_rules('picture', 'Picure', 'required');

    if ($this->form_validation->run() === FALSE)
    {
        $this->load->view('templates/header2', $data);
        $this->load->view('admin/update', $data);
        $this->load->view('templates/footer2');

    }
    else
    {
        $this->admin_model->update_products();
        $this->load->view('admin/success');
    }
}

function input($id = 0)
{
    $this->load->helper('form');  
    $this->load->helper('html');  
    if($this->input->post('mysubmit'))
    {
        $this->admin_model->update_products();
    }
    else
    {
        $this->admin_model->set_products();
    }
    if((int)$id > 0)
    {

        $query = $this->admin_model->get($id);
        $data['id']['value'] = $query['id'];
        $data['title']['value'] = $query['title'];
        $data['price']['value'] = $query['price'];
        $data['description']['value'] = $query['description'];
        $data['publisher']['value'] = $query['publisher'];
        $data['engine']['value'] = $query['engine'];
        $data['release_date']['value'] = $query['release_date'];
        $data['platform']['value'] = $query['platform'];
        $data['genre']['value'] = $query['genre'];
        $data['picture']['value'] = $query['picture'];

}

  $this->load->view('admin/update',$data);   

}

我的模特:

function get($id){

    $this->load->database();

    $query = $this->db->get_where('products',array('id'=>$id));

    return $query->row_array();        

}

function update_products($data){

  $this->load->helper('url');
  $data = array(

        'id' => $this->input->post('id'),
        'title' => $this->input->post('title'),
        'price' => $this->input->post('price'),
        'description' => $this->input->post('description'),
        'publisher' => $this->input->post('publisher'),
        'engine' => $this->input->post('engine'),
        'release_date' => $this->input->post('release_date'),
        'platform' => $this->input->post('platform'),
        'genre' => $this->input->post('genre'),
        'picture' => $this->input->post('picture')            
      );

  $this->db->where('id',$id);
  $this->db->update('products',$data);  
}

我的观点:

<h2>Create a new product</h2>

<?php echo validation_errors(); ?>

<?php echo form_open('admin/update') ?>

<input type="hidden" value=<?php echo '"' . $id['value'] . '"' ?>>

<label for="title">Title</label>
<input type="text" name="title" value=<?php echo '"' . $title['value'] . '"' ?>><br><br>

<label for="price">Price</label>
<input type="number" step="any" name="price" value=<?php echo '"' . $price['value'] . '"' ?>><br><br>

<label for="description">Description</label>
<textarea name="description"><?php echo '"' . $description['value'] . '"' ?></textarea><br><br>

<label for="publisher">Publisher</label>
<input type="text" name="publisher" value=<?php echo '"' . $publisher['value'] . '"' ?>><br><br>

<label for="engine">Engine</label>
<input type="text" name="engine" value=<?php echo '"' . $engine['value'] . '"' ?>><br><br>

<label for="release_date">Release date</label>
<input type="date" name="release_date" value=<?php echo '"' . $release_date['value'] . '"' ?>><br><br>

<label for="platform">Platform</label>
<input type="text" name="platform" value=<?php echo '"' . $platform['value'] . '"' ?>><br><br>

<label for="genre">Genre</label>
<input type="text" name="genre" value=<?php echo '"' . $genre['value'] . '"' ?>><br><br>

<label for="picture">Picture</label>
<input type="text" name="picture" value=<?php echo '"' . $picture['value'] . '"' ?>><br><br>

<input type="submit" name="mysubmit" value="submit">

提交后的结果:

制作新产品

遇到PHP错误

严重性:注意

消息:未定义的变量:id

文件名:admin / update.php

行号:7

&#34;&#34;&GT;标题
遇到PHP错误

严重性:警告

消息:非法字符串偏移&#39;值&#39;

文件名:admin / update.php

行号:10

&#34; U&#34;&GT;

价格
遇到PHP错误

严重性:注意

消息:未定义的变量:价格

文件名:admin / update.php

行号:13

&#34;&#34;&GT;

说明

出版商
遇到PHP错误

严重性:注意

消息:未定义的变量:publisher

文件名:admin / update.php

行号:19

&#34;&#34;&GT;

发动机
遇到PHP错误

严重性:注意

消息:未定义的变量:引擎

文件名:admin / update.php

行号:22

&#34;&#34;&GT;

发布日期
遇到PHP错误

严重性:注意

消息:未定义的变量:release_date

文件名:admin / update.php

行号:25

&#34;&#34;&GT;

平台
遇到PHP错误

严重性:注意

消息:未定义的变量:platform

文件名:admin / update.php

行号:28

&#34;&#34;&GT;

流派
遇到PHP错误

严重性:注意

消息:未定义的变量:流派

文件名:admin / update.php

行号:31

&#34;&#34;&GT;

照片
遇到PHP错误

严重性:注意

消息:未定义的变量:图片

文件名:admin / update.php

行号:34

&#34;&#34;&GT;

1 个答案:

答案 0 :(得分:0)

你的代码中有很多问题/错误,但我会尝试标记并修复它们。让我们开始......

在以下行之后的input($id = 0)方法中:

$query = $this->admin_model->get($id);

你有这样的代码:

$data['id']['value'] = $query['id'];

将所有内容更改为:

$data['id'] = $query['id'];
$data['title'] = $query['title'];
// more...

从所有这些中移除['value']。然后在你的view中你有变量(数组),如:

$id['value']
$title['value']

从这些中删除['value'],并为每个变量/字段使用$title等等。然后在您的视图中再次进行更改:

<?php echo form_open('admin/update') ?>

对此:

<?php echo form_open('admin/update/' . $id) ?>

同时删除<input type="hidden" value=<?php echo '"' . $id['value'] . '"' ?>>,最后在update_products方法中,将其更改为:

function  update_products($id)
{
    //...
}

来自$data方法中的update_products数组;删除这个:

'id' => $this->input->post('id'),

希望这会有效,但你有很多错误,所以不确定我是否可以将它们全部标记出来。试一试。

更新:您从update_products致电update,因此您应该将id传递给update方法,以便进行更改,添加$id参数:

public function update($Id)
{
    //...
}

然后在这个调用update_products方法的方法中,$id用它传递$this->admin_model->update_products($id);

{{1}}