Elven6 Posted August 26, 2010 Share Posted August 26, 2010 For some reason my for loop only seems to be storing some data in my fields, most of the time the data is not true but simply duplicating one of the entries. So instead of having a string of 1,2,3,4 for instance it would store all of them as 4. The entires are selected using fields in a multiple select box, in theory of someone choses 10 unique entries from the "Systems" category all 10 should be made into rows in the MySQL table. Any idea why this is happening? The form, if ($type == "games") { echo "<tr><td>".$name."</td><td><select name='".$name."_".$i."'[] multiple='multiple'><option value='' selected>--------</option>"; $sqla = mysql_query("SELECT * FROM ".$pre."games ORDER BY `name` ASC") or die(mysql_error()); while($row2a = mysql_fetch_array($sqla)) { $system = mysql_fetch_array(mysql_query("SELECT * FROM ".$pre."systems WHERE id = '".$row2a[system]."'")); echo "<option value='".$row2a[id]."'>".$row2a[name]." - ".$system[name]."</option>"; } echo "</select></td></tr>"; } if ($type == "system") { echo "<tr><td>".$name."</td><td><select name='".$name."_".$i."'[] multiple='multiple'><option value='' selected>--------</option>"; $sqlb = mysql_query("SELECT * FROM ".$pre."systems ORDER BY `name` ASC") or die(mysql_error()); while($row2b = mysql_fetch_array($sqlb)) { echo "<option value='".$row2b[id]."'>".$row2b[name]."</option>"; } echo "</select></td></tr>"; } The PHP code, while($row = mysql_fetch_array($query)) { $name = "$row[name]"; for ($i = 0; $i < count($_POST["systems_$i"]); $i++) { mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, 'systems', '".$_POST["systems_$i"]."', '".$fetch[0]."', 'content')"); } for ($i = 0; $i < count($_POST["games_$i"]); $i++) { mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, 'games', '".$_POST["games_$i"]."', '".$fetch[0]."', 'content')"); } Quote Link to comment https://forums.phpfreaks.com/topic/211834-for-loop-only-storing-some-entries-in-database/ Share on other sites More sharing options...
Elven6 Posted August 28, 2010 Author Share Posted August 28, 2010 Anyone able to help? Haven't been able to find the bug yet and am on the verge of burning out on it. =/ Quote Link to comment https://forums.phpfreaks.com/topic/211834-for-loop-only-storing-some-entries-in-database/#findComment-1104602 Share on other sites More sharing options...
PFMaBiSmAd Posted August 28, 2010 Share Posted August 28, 2010 Your code appears to be using an $i variable to make/access a sequence of named fields (which is never a good idea) AND you are attempting to make that field an array (which is the best way, however, your [] are outside of the field name attribute and probably don't work.) While that might make sense for sets of sets of data, your form processing code clearly cannot work because you are using the same $i variable in the for(){} loops that you are using to get the count() as the limit of the loop. You need to start by defining what you want as your data, then make sure your form is correct, and that the data submitted to your form processing code is what you expect. Quote Link to comment https://forums.phpfreaks.com/topic/211834-for-loop-only-storing-some-entries-in-database/#findComment-1104607 Share on other sites More sharing options...
Elven6 Posted August 28, 2010 Author Share Posted August 28, 2010 Your code appears to be using an $i variable to make/access a sequence of named fields (which is never a good idea) AND you are attempting to make that field an array (which is the best way, however, your [] are outside of the field name attribute and probably don't work.) While that might make sense for sets of sets of data, your form processing code clearly cannot work because you are using the same $i variable in the for(){} loops that you are using to get the count() as the limit of the loop. You need to start by defining what you want as your data, then make sure your form is correct, and that the data submitted to your form processing code is what you expect. Thanks for the response, would you recommend something like this to solve the issue? for ($ia = 0; $ia < count($_POST["systems_$i"]); $ia++) { mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, 'systems', '".$_POST["systems_$i"]."', '".$fetch[0]."', 'content')"); } I also ended up moving the [] to the left of the ' which seems to have somewhat fixed the issue but now the variables are simply storying as "array". For the $type areas in the form, what do you recommend in place of the $i? Thanks again, Quote Link to comment https://forums.phpfreaks.com/topic/211834-for-loop-only-storing-some-entries-in-database/#findComment-1104617 Share on other sites More sharing options...
Elven6 Posted September 3, 2010 Author Share Posted September 3, 2010 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/211834-for-loop-only-storing-some-entries-in-database/#findComment-1106854 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.