Jump to content

For Loop Only Storing Some Entries in Database


Elven6

Recommended Posts

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')");
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.