Reputation: 139
Despite my best efforts at following the CI 3.1.0 documentation concerning standard CRUD update procedure, I'm never able to have the targeted db elements update with information entered into my edit form. Currently, this is the code determining my update flow (in its' entirety);
EDIT FORM (edit.php - view file)
<div id="container">
<h1>Edit Post: <?php echo $post[0]['title']; ?></h1>
<?php if($msg = $this->session->flashdata("message")): ?>
<p class="success">
<?php echo $msg; ?>
</p>
<?php endif; ?>
<form action="<?php echo base_url('submit/update'); ?>" method="POST">
<p>
<label for="title">Title edit:</label>
<input type="text" name="post[title]" id="title" value="<?php echo $post[0]['title'];?>"/>
</p>
<p>
<label for="article_text">Content edit:</label>
<textarea name="post[article]" id="article" rows="5" cols="40"><?php echo $post[0]['article_text']; ?></textarea>
</p>
<p>
<label for="category">Category edit:</label>
<SELECT name="post[category]" id="category" value="<?php echo $post[0]['category'];?>">
<OPTION>CODING</OPTION>
<OPTION>DOWNLOADS</OPTION>
<OPTION>HUMOR</OPTION>
<OPTION>IMAGES</OPTION>
<OPTION>INFO</OPTION>
<OPTION>MMO</OPTION>
<OPTION>MULTIMEDIA</OPTION>
<OPTION>RANDOM</OPTION>
<OPTION>REVIEWS</OPTION>
<OPTION>TUTORIALS</OPTION>
</SELECT>
</p>
<p>
<input type="hidden" name="post[article_id]" id="article_id" value="<?php echo $post[0]['article_id'];?>" />
<input class="btn btn-info btn-block" type="submit" value="Edit article" name="submit">
</p>
</form>
</div>
If anyone's wondering why I don't just append $article_id
to my form action
,
it's because, for some reason, it keeps nulling the elements I'm targeting for update. I'm packing $this->output->enable_profiler(TRUE);
in the submit controller parent::__construct();
to make sure that the correct data is being pulled into the edit page, targeting the correct $article_id
for editing. This is the form prior to any attempt to update it;
Submit Controller Update Function (submit.php)
function update()
{
$this->load->model('article_model');
$article_id = $this->input->post('article_id')
$this->Article->updateData($article_id);
}
Article Model Update Function
//UPDATE DATA FROM TINYMCE INTO DB TABLE
function updateData($article_id)
{
$title = $this->input->post('title');
$article = $this->input->post('article_text');
$category = $this->input->post('category');
$data = array(
'title'=> $title,
'article_text'=>$article,
'category'=>$category
);
$this->db->set('date', 'NOW()', FALSE);
$this->db->where('article_id', $this->input->post('article_id'));
$this->db->update('articles', $data);
redirect('submit/manage', 'refresh');
exit;
}
I'd previously experimented with $this->db->replace
, but that errored out as inserting NULL
into the db for those targeted elements. I've tried about all I can as enabled by the CI 3 docs and SO answer inventory. And neither my WAMP solution error logs nor the CI application log are throwing me any bones. Fresh out of ideas on this issue...
UPDATE 1
Thanks to something @DFriend mentioned in the comments, I was able to get an error message that let me know that the set
method was required for the update process. I saw another notation while I was on the hunt here @ SO in the vein of;
$this->db->set('title', $title);
$this->db->set('article_text', $article);
$this->db->set('category', $category);
$this->db->where('article_id', $article_id);
$this->db->update('articles');
But, alas, no joy as I get the same result as last coding attempt where I'm shunted back to the CRUD article list with nary a change in sight. Going to be a long night...
DAYBREAK
Sometimes it's fun to wander around in circles all night (not...), through the labyrinth of documentation, really trying to hash out its' vagaries. One of the few nuggets I came away with was $this->output->enable_profiler(TRUE);
. Intuitively, I removed the redirect()
call at the end of the updateData()
function, and lo and behold...
So it's confirmed that there is postdata, but something is causing only NULL
entries for those targeted elements. This a lot better situation to be in than the previous. Would be great if the Profiler could also locate the NULL
sourcing for me (lol!), but then again, that's my job...
Upvotes: 0
Views: 204
Reputation: 139
HAAAAAAAAAAAAAAAAAAAAAAAA....Nailed it!
Something smacked me in the back of the head to compare the submission forms for both the submit
and edit
pages. As I'd been customizing a tutorial for my own usage, I neglected to change form elements to reflect db column residents;
name="post[title]" != name="title"
name="post[article]" != name="article_text"
name="post[category]" != name="category"
It was just these three elements stuffing it up for me, and blocking the postdata from the form from inserting into the database. Now, there is joy in my world...This is the initial Article Manager before;
And now, after application of my code edit...
@DFriend, I'm going to be upvoting your answer as I believe it was ultra helpful (along with those thought-provoking comments from you as well) to me and will be of use to others down the line as this answer is searched out in the future. For those who may be struggling with this issue, this is my final overview;
EDIT FORM (edit.php - view file)
<div id="container">
<h1>Edit Post: <?php echo $post[0]['title']; ?></h1>
<?php if($msg = $this->session->flashdata("message")): ?>
<p class="success">
<?php echo $msg; ?>
</p>
<?php endif; ?>
<form action="<?php echo base_url('submit/update'); ?>" method="POST">
<p>
<label for="title">Title edit:</label>
<input type="text" name="title" id="title" value="<?php echo $post[0]['title'];?>"/>
</p>
<p>
<label for="article_text">Content edit:</label>
<textarea name="article_text" id="article_text" rows="5" cols="40"><?php echo $post[0]['article_text']; ?></textarea>
</p>
<p>
<label for="category">Category edit:</label>
<SELECT name="category" id="category" value="<?php echo $post[0]['category'];?>">
<OPTION>CODING</OPTION>
<OPTION>DOWNLOADS</OPTION>
<OPTION>HUMOR</OPTION>
<OPTION>IMAGES</OPTION>
<OPTION>INFO</OPTION>
<OPTION>MMO</OPTION>
<OPTION>MULTIMEDIA</OPTION>
<OPTION>RANDOM</OPTION>
<OPTION>REVIEWS</OPTION>
<OPTION>TUTORIALS</OPTION>
</SELECT>
</p>
<p>
<input type="hidden" name="article_id" id="article_id" value="<?php echo $post[0]['article_id'];?>" />
<input class="btn btn-info btn-block" type="submit" value="Edit article" name="submit">
</p>
</form>
</div>
Submit Controller Update Function (submit.php)
function update()
{
$this->load->model('article_model');
$article_id = $this->input->post('article_id');
$this->Article->updateData($article_id);
}
Article Model Update Function
function updateData($article_id)
{
$title = $this->input->post('title');
$article = $this->input->post('article_text');
$category = $this->input->post('category');
$data = array(
'title'=> $title,
'article_text'=>$article,
'category'=>$category,
'date'=> date('Y-m-d H:i:s')
);
$this->db->set('title', $title);
$this->db->set('article_text', $article);
$this->db->set('category', $category);
$this->db->where('article_id', $article_id);
$this->db->update('articles', $data);
redirect('submit/manage', 'refresh');
exit;
}
The exhilaration of nailing this has driven away the need for sleep (lol!). Let's see how far I get before the Sandman catches me at the desk in the back of my head. @DFriend, thanks again for your eyes and those deft comments. Couldn't have gotten here without your assistance...
Tying Up Loose Ends...
Also for the benefit of anyone stumbling on this post: had to clone the article model edit
function so that I could point it at the delete.php
page (itself a clone of the submit.php
page) with the form action
of base_url('submit/trash')
which took out the garbage for me effortlessly, redirecting to the article manager minus the deleted article.... And the last bit (earning me a functional application, to boot);
DELETE FORM (delete.php - view page)
<div id="container">
<h1>Delete Post: <?php echo $post[0]['title']; ?></h1>
<form action="<?php echo base_url('submit/trash'); ?>" method="POST">
<p>
<label for="title">Title</label>
<input size="25" type="text" name="title" id="title" value="<?php echo $post[0]['title'];?>" readonly />
</p>
<p>
<label for="article_text">Content</label>
<textarea readonly name="article_text" id="article_text" rows="12" cols="120"><?php echo $post[0]['article_text']; ?></textarea>
</p>
<p>
<label for="category">Category</label>
<SELECT disabled name="category" id="category" value="">
<OPTION><?php echo $post[0]['category'];?></OPTION>
</SELECT>
</p>
<p>
<input type="hidden" name="article_id" id="article_id" value="<?php echo $post[0]['article_id'];?>" />
<input class="btn btn-info btn-block" type="submit" value="Delete article" name="submit">
</p>
</form>
</div>
Submit Controller Delete Function (submit.php)
function delete() //so we can show delete.php view page
{
$this->load->model('article_model');
$article_id = $this->uri->segment(3);
$post = $this->Article->getArticle($article_id);
if(!$post)
{
redirect('submit/manage', 'refresh');
exit;
}
$data['post'] = $post;
$this->load->view('delete',$data);
}
Delete Form "action" function
function trash()
{
$this->load->model('article_model');
$article_id = $this->input->post('article_id');
$this->Article->deleteData($article_id);
}
Article Model Delete Function
function deleteData($article_id)
{
$this->db->where('article_id', $article_id);
$this->db->delete('articles');
redirect('submit/manage', 'refresh');
exit;
}
I really hope this post helps someone in the future. It's certainly helped me (lol!)...
Upvotes: 0
Reputation: 8964
Total shot in the dark as all the other bases seemed to be covered.
Suggested revision
//UPDATE DATA FROM TINYMCE INTO DB TABLE
function updateData($article_id)
{
$title = $this->input->post('title');
$article = $this->input->post('article_text');
$category = $this->input->post('category');
$data = array(
'title'=> $title,
'article_text'=>$article,
'category'=>$category,
'date'=> date("Y-m-d H:i:s") //same data as NOW() only via PHP
);
//Here's the shot in the dark - set() is somehow screwing things up
//$this->db->set('date', 'NOW()', FALSE);
$this->db->where('article_id', $article_id); //you passed $article_id into the function so use it
$this->db->update('articles', $data);
redirect('submit/manage', 'refresh');
//redirect ends with a call to exit so the next line would never execute anyway
//exit;
}
Upvotes: 1