Welcome Guest, Not a member yet? Register   Sign In
Pls help me! Why I can't update database!?
#1

[eluser]Old Tam[/eluser]
Here is the Controller :ecms.php
Code:
<?php
class Ecms extends Controller {
function Ecms()
{
  parent::Controller();
  
  $this->load->helper('url');
  $this->load->helper('form');
  
}
function index()
{
  $this->load->database();
  $this->load->view('ecms_view',$data);
}

function product()
{
  $this->load->database();

  $data['query']=$this->db->get('product');
  
  $this->load->view('product_view',$data);
}

function product_insert()
{
  $this->load->database();
  $this->db->insert('product',$_POST);
  
  redirect('ecms/product/');
}

function product_edit()
{
  $this->load->database();
  $this->db->where('id',$this->uri->segment(3));
  $data['query']=$this->db->get('product');
  
  $this->load->view('product_edit',$data);
}

function product_update()
{
  $this->load->database();

  $this->db->set('products',$_POST['products_name']);
  $this->db->where('id',$this->uri->segment(3));
  $this->db->update('product');
  
  redirect('ecms/product/');
}
}
?>

Here is the View:product_edit.php
Code:
<html>
<head>
<title><?=$title?></title>
</head>
<body>
<h1>&lt;?=$heading?&gt;</h1>
<p>&lt;?=anchor('ecms','Home');?&gt;</p>

&lt;?=form_open('ecms/product_update');?&gt;
&lt;?php if($query->num_rows()==1):?&gt;
&lt;?php foreach($query->result() as $row):?&gt;
<p>Products:&lt;input type="text" value=&lt;?=$row-&gt;products;?&gt; name="products_name"/></p>
&lt;?php endforeach;?&gt;
&lt;input type="submit" value="update"/&gt;
&lt;?php else:?&gt;
&lt;?php echo"error!";?&gt;
&lt;?php endif;?&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Pls help me! Why I can't update database!?
#2

[eluser]wiredesignz[/eluser]
Just a quick guess.

uri->segment(3) looks empty, form action has no 3rd segment.
#3

[eluser]Old Tam[/eluser]
[quote author="wiredesignz" date="1201324003"]Just a quick guess.

uri->segment(3) looks empty, form action has no 3rd segment.[/quote]

thank you very much!

But The uri->segment(3) is not empty,it contain in the url of the page 'product_edit.php'

the code has no error,but,it can't update the datebase,How can I do!? Pls Help me!
#4

[eluser]wiredesignz[/eluser]
Your form is not passing the `id` value to the product_update method. Unless `id` == 'product_update'.

Good luck. Tongue
#5

[eluser]Old Tam[/eluser]
[quote author="wiredesignz" date="1201341523"]Your form is not passing the `id` value to the product_update method. Unless `id` == 'product_update'.

Good luck. Tongue[/quote]

Thank you Smile

I put the code $this->uri->segment(3) in the form_hidden function of the view "product_edit.php" .
It can update now!

Thank you for your help!thank you!
#6

[eluser]crispinatari[/eluser]
[quote author="Old Tam" date="1201370074"][quote author="wiredesignz" date="1201341523"]Your form is not passing the `id` value to the product_update method. Unless `id` == 'product_update'.

Good luck. Tongue[/quote]

Thank you Smile

I put the code $this->uri->segment(3) in the form_hidden function of the view "product_edit.php" .
It can update now!

Thank you for your help!thank you![/quote]

Hi Tam!

can you please show your working code of the view and controller now that it works and how you set out the code because i'm having a similar issue with updating that won work, but i think it may also be an sql error for me, though i'm not sure.




Theme © iAndrew 2016 - Forum software by © MyBB