![](/img/trans.png)
[英]Codeigniter - Update email only if the email doesn't exist in the database
[英]codeigniter doesn't update the database
我正在嘗試使用表單中的數據更新數據庫,表單中填充了daatabase中的數據。 但是,當我單擊提交時它給出了錯誤。 有人可以幫忙嗎?
我的控制器:
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
“”>標題
遇到PHP錯誤
嚴重程度:警告
消息:非法字符串偏移量“值”
文件名:admin / update.php
行號:10
“ U“>
價錢
遇到PHP錯誤
嚴重程度:注意
消息:未定義的變量:價格
文件名:admin / update.php
行號:13
“”>
描述
發行人
遇到PHP錯誤
嚴重程度:注意
消息:未定義的變量:發布者
文件名:admin / update.php
行號:19
“”>
發動機
遇到PHP錯誤
嚴重程度:注意
消息:未定義的變量:引擎
文件名:admin / update.php
行號:22
“”>
發布日期
遇到PHP錯誤
嚴重程度:注意
消息:未定義的變量:release_date
文件名:admin / update.php
行號:25
“”>
平台
遇到PHP錯誤
嚴重程度:注意
消息:未定義的變量:平台
文件名:admin / update.php
行號:28
“”>
類型
遇到PHP錯誤
嚴重程度:注意
消息:未定義的變量:體裁
文件名:admin / update.php
行號:31
“”>
圖片
遇到PHP錯誤
嚴重程度:注意
消息:未定義的變量:圖片
文件名:admin / update.php
行號:34
“”>
您的代碼中有很多問題/錯誤,但是我將嘗試標記並修復所有問題/錯誤。 開始吧...
在以下行之后的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
調用update_products
,因此您應該將id
傳遞給update
方法,以便進行更改,並添加$id
參數:
public function update($Id)
{
//...
}
然后在您要調用update_products
方法的此方法中,將$id
傳遞給它,如下所示:
$this->admin_model->update_products($id);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.