Jump to content

foreach loop for queries


jimfog

Recommended Posts

I am making a booking app where the user will have the option to select services and a specific booking might br associated

with 1 or more services.

I have made the PHP so that the services are passed to an array...my problem is how to insert this array into a specific db table,given the fact(as stated above) that the length of the array varies.

 

I have concluded to something like that and I want to hear your opinion about it:

foreach($array as $value){run query}

that means(I think) that if the array has 2 members for example(2 services selected) then 2 queries will be executed.

What do you think about the overall logic?

Link to comment
Share on other sites

here is the problem I am facing-see the code first:

  foreach ($services as $value) {         $result1=$connection->query('insert into appoint_servi_chosen(app_ID,service_ID)           values("'.$connection->insert_id.'","'.$value.'")');                  }

Let us suppose the above $services array has 2 members.Only the first member is stored in the db.

I suppose there must be a problem with $result1...since I get an error message regarding this variable.

I have this piece of error code:

if(!$result1)          {        echo 'problem with result1.';         return false;                   }else

The browser outputs 'problem with result1'.

So the first query(inside the loop) gets executed,the second is not(that is why the second array member does not enter the db).

I am searching the cause of that.

 

Any ideas?

Link to comment
Share on other sites

yes you are right for the error message-I forget it from time to time.So after investigation I concluded the following:

 

As you notice in the above code I use the insert_id method to put in the appoint_servi_chosen table in the appID column the last generated ID produced by the previous INSERT query(not shown in the above code).

 

But when the loop runs and the array has 2 members(not always case) the 2nd query which is going to correspond to the 2nd array member,its insert_id method will be based on the previous array member query in which case there is not a generated ID at all-in fact I do not know what is sent in the db at all.

The generated id is create only in the first query-not part of the loop.Here is it:

 $result = $connection->query('insert into appointments(name,startDate,apps_origin,staffID) values                      ("'.$name.'","'.$start.'","'.$origin.'","'.$staff.'")');

Did you understand the nature of the problem?

Do you have any idea how I could solve this?

Link to comment
Share on other sites

If you need to save the ID to keep using it in the loop, then save the ID before the loop. I'm not sure what the problem is.

Yes that is one possible solution...although this might mean an extra SELECT query,I am not sure about this last one though.

Link to comment
Share on other sites

yes...now we're talking...the solution worked.No SELECT query needed.

Link to comment
Share on other sites

I have to admit that I am confused about why you don't come to these conclusions on your own. If you have access to the ID, but if you go through one iteration of the loop and the ID changes, why does it not occur to you to save that ID before the loop?

Link to comment
Share on other sites

because some times even the obvious is not...so obvious.That is true for everyone,not just me.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...