Muliple insert into DB Loop |
[eluser]RaZoR LeGaCy[/eluser]
Hello everyone, What I am trying to do is take on form with three values and create a loop. Then pass the loop values into an insert into DB. Code: <center> So that should creat an array. Now I have my insert like this but there is something that I am missing. Code: for ( $i = 0; $i <= 5; $i++) { In other words I want to insert the values into the DB using a for loop or a foreach. I am confusing myself on which to use and how to implement it. Thanks everyone
[eluser]GSV Sleeper Service[/eluser]
doing 5 separate inserts isn't very efficient, you could do this using just one insert. something like Code: INSERT INTO table (foo, bar) ..and I've just spotted that you've got a redirect within your loop!
[eluser]RaZoR LeGaCy[/eluser]
I want to check if there are any duplicates and if not, then insert the values
[eluser]charlie spider[/eluser]
as GSV Sleeper Service said, your loop won't work because of the redirect. but on top of that, you may find that your php loop will outpace your db's ability to check for existing records and then perform inserts. do you really think people are going to submit 5 movie reviews at a time ? how many horror flicks can a person watch in a night ? hee hee just kidding... another thing i noticed is that the line: Quote:if ($namecheck = '0') should prolly be: Code: if ($namecheck == '0') else your just setting a value for $namecheck but a better way to do it would be to check for the existing movie using a select instead of using COUNT, then just evaluate whether the query returned false or not. so: Code: $this->db->like(array('cid' => '1', 'title' => $title, 'year' => $year)); instead of Quote:$namecheck = $this->db->count_all_results(); i think that might be faster.
[eluser]GSV Sleeper Service[/eluser]
[quote author="RaZoR LeGaCy" date="1206774901"]I want to check if there are any duplicates and if not, then insert the values[/quote] ok, in the the example below foo is your primary key Code: INSERT INTO table (foo, bar) ON DUPLICATE KEY UPDATE is a most useful construct. http://dev.mysql.com/doc/refman/5.0/en/i...icate.html
[eluser]RaZoR LeGaCy[/eluser]
[quote author="charlie spider" date="1206783602"]as GSV Sleeper Service said, your loop won't work because of the redirect. but on top of that, you may find that your php loop will outpace your db's ability to check for existing records and then perform inserts. do you really think people are going to submit 5 movie reviews at a time ? how many horror flicks can a person watch in a night ? hee hee just kidding... another thing i noticed is that the line: Quote:if ($namecheck = '0') should prolly be: Code: if ($namecheck == '0') else your just setting a value for $namecheck but a better way to do it would be to check for the existing movie using a select instead of using COUNT, then just evaluate whether the query returned false or not. so: Code: $this->db->like(array('cid' => '1', 'title' => $title, 'year' => $year)); instead of Quote:$namecheck = $this->db->count_all_results(); i think that might be faster.[/quote] That didn't work because it was a fuzzy match but this worked Code: $query = $this->db->get_where('hellhorror_reviews', array('cid' => '1', 'title' => $title, 'year' => $year));
[eluser]RaZoR LeGaCy[/eluser]
[quote author="GSV Sleeper Service" date="1206796661"][quote author="RaZoR LeGaCy" date="1206774901"]I want to check if there are any duplicates and if not, then insert the values[/quote] ok, in the the example below foo is your primary key Code: INSERT INTO table (foo, bar) ON DUPLICATE KEY UPDATE is a most useful construct. http://dev.mysql.com/doc/refman/5.0/en/i...icate.html[/quote] I read this but I have three fields in table where they must all equal the same in order to signify as a duplicate ex. if (cid = 1 and title = title and year = year) then is duplicate
[eluser]GSV Sleeper Service[/eluser]
make a unique index on your table using those three fields, something like Code: CREATE UNIQUE INDEX myindex ON your_table (cid, title, year) |
Welcome Guest, Not a member yet? Register Sign In |