Welcome Guest, Not a member yet? Register   Sign In
[SOLVED]How to make a conditional insert in multiple tables?
#1

[eluser]earlyriser[/eluser]
I want to make a conditional insert in multiple tables.
My tables are:
WINES: id, name, year.
WINE_COMMENTS: id, user_id, wine_id, rating.
and some user tables from Freak Auth

Then, my form asks for a wine name, a wine year, a rating and it has a hidden field with the logged user.

If the wine name is not in the database, the controller needs to
-add a wine: id (incremental), name (from the form), year (from form)
-add a comment: id (incremental), user_id (form), wine_id (recent added wine.id), rating(form).

If the wine name is in the database, the controller needs just to
-add a comment: id (incremental), user_id (form), wine_id (recent result wine.id), rating(form).

This is my form
Code:
<?=form_open('wine/comment_insert');    ?>
<?=form_hidden('user_id', getUserProperty('id')); ?>
<p>name&lt;input type='text' name='name'/&gt;&lt;/p>
<p>rating&lt;input type='text' name='year'/&gt;&lt;/p>
<p>year&lt;input type='text' name='rating'/&gt;&lt;/p>
<p>&lt;input type="submit" value="Submit Comment"&gt;&lt;/p>
&lt;/form&gt;

Code:
function comment_insert()
    {
        //check if the wine is in database
        $this->db->select('*');
        $this->db->from('wines');
        $this->db->where('name', $name);
        $data['query']= $this->db->get();
        if ($query->num_rows() > 0) //if it was found in wines.names
        {
            //add new comment
            insert (wine_comments.user_id)    
            insert (wine_comments.rating)
            insert (wine_comments.wine_id)
        }
        else    //if it wasnt found in wines.names
        {
            //add new wine
            insert (wines.name)        
            insert (wines.year)
            
            //add new comment
            insert (wine_comments.user_id)    
            insert (wine_comments.rating)
            insert (wine_comments.wine_id)
        }
    }    
}

Please help me turning this pseudocode in the correct code.
I have made the Blog tutorial, but it doesn't help me with this multiple insert.


Messages In This Thread
[SOLVED]How to make a conditional insert in multiple tables? - by El Forum - 10-02-2008, 12:22 PM



Theme © iAndrew 2016 - Forum software by © MyBB