Page 1 of 1

foreach or while or neither?

Posted: Fri Sep 22, 2006 11:32 am
by blacksnday
After a mysql insert, I have some code that needs to update other sections.
It may or may not need to update 2 or more sections.
my problem is that I cannot figure out how to get it to update more then once.
I normally use foreach or while loops but in this case nothing I have tried seems to work.

Example:

Code: Select all

//Insert that the below will base info from:
mysql_query ("INSERT INTO table (foo, boo) VALUES ('$foo', '$boo')");
$post_id = mysql_insert_id();

//This needs to run at least once, or multiple times depending on if there are more then 1 field:
//How can i get this part to insert more then once for each field if needed?
mysql_query ("INSERT INTO field (field_id, post_id, content) VALUES ('$field_id', '$post_id', '$content'')");

Posted: Fri Sep 22, 2006 11:58 am
by feyd
I think we'll need some more explaination before being able to help as your post makes almost no sense when compared to your code.

Re: foreach or while or neither?

Posted: Fri Sep 22, 2006 12:05 pm
by aquanutz
blacksnday wrote:After a mysql insert, I have some code that needs to update other sections.
It may or may not need to update 2 or more sections.
my problem is that I cannot figure out how to get it to update more then once.
I normally use foreach or while loops but in this case nothing I have tried seems to work.

Example:

Code: Select all

//Insert that the below will base info from:
mysql_query ("INSERT INTO table (foo, boo) VALUES ('$foo', '$boo')");
$post_id = mysql_insert_id();

//This needs to run at least once, or multiple times depending on if there are more then 1 field:
//How can i get this part to insert more then once for each field if needed?
mysql_query ("INSERT INTO field (field_id, post_id, content) VALUES ('$field_id', '$post_id', '$content'')");
Do you mean that you will need that second query to run depending on the results of the first query, like if you bring back multiple entries for 'foo' or something, if so, try something like this:

Code: Select all

mysql_query ("INSERT INTO table (foo, boo) VALUES ('$foo', '$boo')");
$result = mysql_query($query) or die("Invalid query: " . mysql_error());

while($d = mysql_fetch_array($result)
{
      $query2 = mysql_query ("INSERT INTO field (field_id, post_id, content) VALUES ('$field_id', '$post_id', '$content'')");
      $result2 = mysql_query($query2) or die("Invalid query: " . mysql_error());
}
That will go through each result of the first query, lets say there are 5 entries taht are pulled back, then the second query will be executed five times... Hope this sheds some light on your problem.

Re: foreach or while or neither?

Posted: Fri Sep 22, 2006 12:21 pm
by blacksnday
aquanutz wrote:
blacksnday wrote:After a mysql insert, I have some code that needs to update other sections.
It may or may not need to update 2 or more sections.
my problem is that I cannot figure out how to get it to update more then once.
I normally use foreach or while loops but in this case nothing I have tried seems to work.

Example:

Code: Select all

//Insert that the below will base info from:
mysql_query ("INSERT INTO table (foo, boo) VALUES ('$foo', '$boo')");
$post_id = mysql_insert_id();

//This needs to run at least once, or multiple times depending on if there are more then 1 field:
//How can i get this part to insert more then once for each field if needed?
mysql_query ("INSERT INTO field (field_id, post_id, content) VALUES ('$field_id', '$post_id', '$content'')");
Do you mean that you will need that second query to run depending on the results of the first query, like if you bring back multiple entries for 'foo' or something, if so, try something like this:

Code: Select all

mysql_query ("INSERT INTO table (foo, boo) VALUES ('$foo', '$boo')");
$result = mysql_query($query) or die("Invalid query: " . mysql_error());

while($d = mysql_fetch_array($result)
{
      $query2 = mysql_query ("INSERT INTO field (field_id, post_id, content) VALUES ('$field_id', '$post_id', '$content'')");
      $result2 = mysql_query($query2) or die("Invalid query: " . mysql_error());
}
That will go through each result of the first query, lets say there are 5 entries taht are pulled back, then the second query will be executed five times... Hope this sheds some light on your problem.
That is almost what I am saying :P
Let me try to explain better:

I have a form that allows for article submission.
Once a user submits the form, the article is stored in its own sql table.
On that form are also custom fields.
The custom fields could be as little as one, or as many as 1++

I have seperate code to insert/update custom fields based on the article submitted.
As of now, I can get all custom fields to display on the article form however I can
only get it to insert/update the last custom field, when I need to get it to insert/update
each time for each custom field--this being due to not being able to get the custom field query to run more then once.

Now the article submission is only ran once no matter what,
and the custom fields update/insert only uses the ID of the article submission to properly
associate the custom field with the proper article.
So I cant use a while loop based on the article submission to update/insert each custom field.

The second query needs to run each time for each custom field available
Each custom field has its own field value, such as:

Code: Select all

<input type="hidden" name="custom_field_id" value="1" />
<input type="text" name="custom_field_content1" />

<input type="hidden" name="custom_field_id" value="2" />
<input type="text" name="custom_field_content2" />
So i would need something like:

Code: Select all

//Insert that the below will base info from: 
mysql_query ("INSERT INTO table (foo, boo) VALUES ('$foo', '$boo')"); 
$post_id = mysql_insert_id(); 

//This of course wont work, but hopefully it illustrates what I am trying to attempt
$fields = $_POST['custom_field_id'];
foreach ($fields as $something)
{
mysql_query ("INSERT INTO field (field_id, post_id, content) VALUES ('$field_id', '$post_id', '$content')");
}
so that each custom field runs the sql INSERT on its own...

hopefully that explains it better and why I cannot use a while loop based on the first query?

Posted: Fri Sep 22, 2006 12:27 pm
by mibocote
This should work, but I might be a little off since I haven't done this in awhile:

Code: Select all

<input type="text" name="custom_field_content[0]" /> 
<input type="text" name="custom_field_content[1]" />

Code: Select all

$fields = $_POST['custom_field_content'];
foreach ($fields as $id=>$content)
{
// query here
}

Posted: Fri Sep 22, 2006 12:52 pm
by blacksnday
mibocote wrote:This should work, but I might be a little off since I haven't done this in awhile:

Code: Select all

<input type="text" name="custom_field_content[0]" /> 
<input type="text" name="custom_field_content[1]" />

Code: Select all

$fields = $_POST['custom_field_content'];
foreach ($fields as $id=>$content)
{
// query here
}
Thanks a ton! That solved my problem.
I knew it wasn't hard, but my brain is fried right now :D