[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<input type='text' name='name'/></p>
<p>rating<input type='text' name='year'/></p>
<p>year<input type='text' name='rating'/></p>
<p><input type="submit" value="Submit Comment"></p>
</form>
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.