drjmrg8766 2018-04-04 14:54
浏览 130

IF语句不能在while循环PHP中工作?

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?

  • 写回答

2条回答 默认 最新

  • dqw70970 2018-04-04 15:09
    关注

    Try this, and then let us know what the errors are. If it's not inserting, it could be an error within PDO that you need to know of.

        if (!$inserted) {
            $inserted = true;
            try {
                $stmt = $conn->prepare("INSERT into UserRecipes (UserID, RecipeID, Displayed) VALUES (:UserID, :RecipeID, :Displayed) ");
                $stmt->execute(array(
                    ':UserID' => $UserID,
                    ':MealID' => $MealID,
                    ':Displayed' => $Displayed
                ));
            } catch (PDOException $p) {
                print $p;
                exit;
            }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析