Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Why my If Statement not Working in While Loop in PHP Program?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.15k
    Answer it

    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?

     

        <?php
    
    $inserted=false;
    
    while($dbRow=$dbQuery->fetch(PDO::FETCH_ASSOC) and ($cnt < $max))  
        {
             echo "".$dbRow["Name"].
            "<br>"."<img src=images\".$dbRow['Picture']."' width='150' height='150' />"."<br>".$dbRow["Instructions"]."" 
    
    ?><form method="POST"><input type="hidden" name="MealID" value="'<?php.$dbRow['MealID']?>'"> 
    <input type="submit" name="submit" value="Complete" ></form><?
    
    
    $cnt++;
    
    
    $MealID=$dbRow["MealID"];
    $Displayed = false;
    if (!$inserted) {
    $inserted=true;
    $stmt=$conn->prepare("INSERT into UserRecipes (UserID, RecipeID, Displayed) VALUES (:UserID, :RecipeID, :Displayed) "); 
    $stmt->execute(array(
           ':UserID' => $UserID,
           ':MealID' => $MealID,
           ':Displayed' => $Displayed
            )); 
       }
        }
    
    
    ?>

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: