Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

IF Statement not working in while loop PHP?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am trying to stop one part of a while loop from only running ONCE.

I have searched and an IF statement seems to be the only way

The only thing is,when i put IF statement in - it doesn't seem to DO the INSERT statement at all, so nothing is being inserted, it's as if it's stopping before insert statement. As you can see $inserted is declared outside the loop and then changed to true inside so it stops, but it doesn't seem to be doing that.

Without it, it will loop each time the page is refreshed and insert into the database each refresh of the page, although i only need the INSERT statement to run once. I need it in the loop as that's where the information for the insert is.

Can anyone help here?


see below code:
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im trying to understand what your attempting and to understand what your saying, so forgive me if im useless.
firstly I think I see a PHP end tag on line ten far left may not be anything crucial
Also on line 11 far right we have a semi php start tag
(looks messy and i did not recognize short tags for a half hour) i would recommend for clarity not to mix short and long tags in the same page.

If i remember correctly I thing php refreshes the page after submit is clicked so when the whole page (not just your while loop) is refreshed and reran your $inserted variable is reset to false.
Did it work before without the IF statement?

What if you just forgot about the $inserted variable and just used the submit button as a reference like

if (isset($_POST['Completed Recipe']))
{ *your statement here*
}
Should only post upon the submit button
I recall PHP being real funky about renaming variables in loops and in nested statements contrary to what the PHP manual says online.

I also cant quite understand what cnt and max are actually doing? depending on what the max is does this loop run for 1 second or an hour? Most forms dont have limits as such. Dont misunderstand im not criticizing i am just having a hard time following what we are trying to do. Maybe the above submit button post might work better for you I hope if i understand the situation properly.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic