Welcome Guest, Not a member yet? Register   Sign In
Having problems with creating a CRUD..
#1

[eluser]ealonw[/eluser]
Hey CI's!!

Im having problems creating an update and delete function... Take look at my controller and view files and see if you can spot the errors for me..

Here are the problems:

1. the EDIT link doesnt populate the view form 'update_contact_view'
2. the DELETE removes all entries in database and NOT the selected entry... so if you are
interested in wiping out your database you can definitely use this code.. LOL!

Any brains out there that can help me correct the syntax?





*******CONTROLLER-->Contact**********
function update()
{

$data = array(

$this->input->post('fname') => $fname,
$this->input->post('lname') => $lname,
$this->input->post('email') => $email,
$this->input->post('comment') => $comment,



);

$this->db->where('fname', $fname);
$this->load->view('update_contact_view');
}



function delete()
{
$this->db->delete('usercomment', array('id' => $id));
// Produces: DELETE FROM usercomment WHERE id = $id

}

}

*********VIEW-->update_contact_view.php**********
<HTML>
<HEAD>
<TITLE>Update</TITLE>
</HEAD>
<BODY>

<?php echo form_open('contact/update'); ?>
<table border="0">
<tr><td>First Name:</td><td>
&lt;input type="text" name="fname" maxlength="20" value="&lt;? echo "$fname"; ?&gt;"&gt;
</td></tr>
<tr><td>Last Name:</td><td>
&lt;input type="text" name="lname" maxlength="20" value="&lt;? echo "$lname"; ?&gt;"&gt;
</td></tr>
<tr><td>Email:</td><td>
&lt;input type="text" name="email" maxlength="50" value="&lt;? echo "$email"; ?&gt;"&gt;
</td></tr>
</table>
<br><br>
&lt;textarea name="comment" cols="40" rows="5" value="&lt;? echo "$comment"; ?&gt;"&gt;
&lt;/textarea&gt;<br><br>
&lt;input type="submit" name="submit" value="submit"&gt;
&lt;/form&gt;
<br>
<br>
<DIV ALIGN=CENTER>



&lt;/form&gt;
</DIV>
&lt;/BODY&gt;
&lt;/HTML&gt;
#2

[eluser]crumpet[/eluser]
not sure what your trying to do with the first function
if you want to update the data with the new information submitted in the form you need this
Code:
$data = array(
                'table_field' => $_POST['data to go in field']
               );
$this->db->where('id', $_POST['id'] );  //you need to set the variable id somewhere i suggest a hidden field in your form
$this->db->update('table', $data);

for the second function its not working because you don't seem to be setting the $ID variable... again hidden form or something

Code:
$this->db->where('id', $id);
$this->db->delete('table');
#3

[eluser]ealonw[/eluser]
hmmmm.... i dont really understand what ur saying... the ID part I didnt think i needed.. why would i need that if i tell it to use fname POST? should it use id instead?

Im trying to create my own scaffolding... or CRUD.. what's missing in my function and view? the function is suppose to get the data from the database put it in a form to be "POST" back to databse... i can do this in scripts i just dont get the CI way... seems my problem is the view file at the bottom.... any brains out there?

*****************Controller Func****************
function update()

{
//error_reporting(0);
$data = array(
'id' => $id,
'fname' => $fname,
'lname' => $lname,
'email' => $email,
'comment' => $comment,
);

$this->db->where('id', $_POST['id']);
$this->db->update('usercomment', $data);

$this->load->view('update_contact_view');
}



*******************VIEW**************************************

&lt;?php echo
//$hidden = array('id' => '$_POST[id]');

form_open('contact/update');


?&gt;

<table border="0">
<tr><td>First Name:</td><td>
&lt;input type="text" name="fname" maxlength="20" value="what should be here"
</td&gt;</tr>
<tr><td>Last Name:</td><td>
&lt;input type="text" name="lname" maxlength="20" value=""&gt;
</td></tr>
<tr><td>Email:</td><td>
&lt;input type="text" name="email" maxlength="50" value=""&gt;
</td></tr>
</table>
<br><br>
&lt;textarea name="comment" cols="40" rows="5" value=""&gt;
&lt;/textarea&gt;<br><br>
&lt;input type="submit" name="submit" value="submit"&gt;
&lt;/form&gt;
<br>
<br>
<DIV ALIGN=CENTER>



&lt;/form&gt;
</DIV>
#4

[eluser]ealonw[/eluser]
Anyone have any idea why this doesnt work... Please advise.. The idea was just to create a basic CRUD to understand how the frameworks....
#5

[eluser]crumpet[/eluser]
provide the error you are getting.
#6

[eluser]ealonw[/eluser]
A PHP Error was encountered
Severity: Notice

Message: Undefined variable: id

Filename: controllers/contact.php

Line Number: 44

An Error Was Encountered
Error Number: 1062

Duplicate entry '0' for key 1

UPDATE usercomment SET id = NULL, fname = NULL, lname = NULL, email = NULL, comment = NULL WHERE id



This is the error i get when i try to update the data using the EDIT button. the data selected doesnt return back to the form fields...
#7

[eluser]Pascal Kriete[/eluser]
You're not grabbing any data from the form:
Code:
$data = array(
‘id’ => $id,
‘fname’ => $fname,
‘lname’ => $lname,
‘email’ => $email,
‘comment’ => $comment,
);

Where do you define $fname, $lname, $email, and $comment ? Also you don't have an id, because you're not posting an id field. And the reason why it doesn't populate is because you're a) not passing variables to the view, and b) even if you passed the $data - they are not defined.

Ditto for the delete link. You never define $id .
#8

[eluser]ealonw[/eluser]
Geez.... okay... Thats what i was wondering... How can i use the id field in this case? i dont understand how that would work here... So not only is the controller incorrect, but the view is also? Can you point me in the right direction or give me a good example on how I should do it?
#9

[eluser]Pascal Kriete[/eluser]
For the remove function it's pretty straight forward. For each remove link you output, you also add a url segment with the id.

So your link would be: http://www.example.com/comment/delete/5

And your code:
Code:
function delete($id = -1)
{
    $this->db->delete('usercomment', array(’id’ => $id));
}

For the edit field you use the same concept. The slug defines the id and is passed into your function as an argument.
Code:
function edit($id)
{
    $this->load->helper('form');
    
    $query = $this->db->get_where('comments', array('id' => $id));
    $data['comment'] = $query->result();

    if ( count($_POST) > 0 )
    {
         $this->db->where('id', $id);
        $this->db->update('comment', $_POST);
        redirect('');
    }
    else
    {
        $this->load->view('edit_comment', $data);
    }
}

All of the keys in the data array are converted to variables in the view so $data['comment'] becomes $comment.

View:
Code:
&lt;?= form_open('comment/edit/'.$comment->id); ?&gt;

&lt;input type="text" name="comment" maxlength="20" value="&lt;?=$comment-&gt;text ?&gt;">

// Etc.

Obviously, you will want form validation and such, but this should get the idea across.

Hope that helps.

ps. try wrapping the code you post in [ code ] tags, it makes it a lot easier to read
#10

[eluser]crumpet[/eluser]
Another thing. When you set up your table for comments make sure ID is set to auto_increment and primary key.

The reason you use ID is because it will always be unique. If you just say select the post where fname == 'george' and that guy posted 5 times you will get 5 posts. you need a unique identifier : an id.

With teh above options in mysql the id will set itself and you can just use it to refer to entries




Theme © iAndrew 2016 - Forum software by © MyBB