Home  |  About  | Last |  Submit  |  Contact
AllQuests.com




Previous Question:  whole numbers  PHPNext Question:  Login Sessions  PHP
Question Newbie MySQL PHP insert question ( SitePoint Forums PHP )
Updated: 2008-11-23 03:25:03 (4)
Newbie MySQL PHP insert question

Hi,

I've been going through the forums trying to find an answer, but because I am new with PHP I'm just ending up more confused - but I sense the answer out there! So, I thought I'd post with my specific need.

I want to execute an INSERT loop depending on a number value entered in the form. (MySQL)

It's a form to post volunteer openings and how many are needed.

ie: 4 Phone Callers for November 15, 2005.

So I need to execute the INSERT query based on the number needed - in this case INSERT the same opening 4 times.

I tried a WHILE loop, but that didn't work. After rummaging around here, it seems I may need either a 'foreach' loop or an 'array', but the syntax is eluding me.

Thanks in advance for the help!

Answers: Newbie MySQL PHP insert question ( SitePoint Forums PHP )
Newbie MySQL PHP insert question

If you wish to just run an action for a certain number of times, you can use a for() loop.

PHP Code:

for ($i=1; $i<=4; $i++) { do_something; } 

If you wish to go through and array, use foreach(). If you wish to do the action until a condition is met, use while(). I am not quite sure which one would suit your needs best. If the INSERTs are the same every time, you would go with for().

TRISPECTIVE

Newbie MySQL PHP insert question

Will the loop have any problems with the submitted number as the condition?

PHP Code:

for ($i=1; $i<=$number; $i++) { do_something; } 

Yes, the INSERT query is the same each time, it's the submitted number that would change each time and is sent as $number in the form.

WarpNacelle

Newbie MySQL PHP insert question

Quote:
Will the loop have any problems with the submitted number as the condition?
Yes, there could be a problem if $number is less than $i (or maybe if it's ridiculously big). That's why you should validate $number's value before using it in the loop. A very simple form of validation would be :
PHP Code:

if($number < 1)

$number = 1;
Which will make the loop run once if $number contained a zero or a negative value.

hope this helps

mPeror

Newbie MySQL PHP insert question

Okay. $number would be small and at least 1, probably never larger then 5 - so it would look like:

PHP Code:

for ($i=1; $i<=$number; $i++) {

INSERT query runs here
}
That would run the INSERT query i times, where i is what $number has been submitted in the form.

That simple.

WarpNacelle

Previous Question:  whole numbers  SitePoint Forums  PHPNext Question:  Login Sessions  SitePoint Forums  PHP

- Source: Newbie MySQL PHP insert question SitePoint Forums PHP
- Previous Question: whole numbers SitePoint Forums PHP
- Next Question: Login Sessions SitePoint Forums PHP