如果表为空,则运行INSERT查询。ELSE运行UPDATE

时间:2019-04-18 15:49:41

标签: php sql mysqli

我尝试过Google并在网上搜索所有内容。我最终想在表INSERT中运行一个IF查询,该表为空,ELSE运行一个UPDATE查询。

我需要检查表-UserTakingModule,字段-checkPoint对于当前登录的用户是否为空,然后运行插入查询:

  $idUsers = $_SESSION['id'];
  $ModuleID = 5;
  $checkPoint = 1;

        // Preparing and binding my SQL query which inserts a checkpoint number into the table
        $stmt = $conn->prepare ("INSERT INTO `userTakingModule` (`userTakingModuleID`, `idUsers`, `ModuleID`, `checkPoint`) VALUES (NULL, ?, ?, ?)");

        $stmt->bind_param("iii", $idUsers, $ModuleID, $checkPoint);
        $stmt->execute();

但是,如果checkPoint中已经保存了一个整数,则只需对当前登录的用户运行UPDATE查询:

// Else need an update query to run for user that's currently logged in
$stmt = $conn->prepare ("UPDATE `userTakingModule` SET `checkPoint` = ? WHERE `userTakingModule`.`userTakingModuleID` = ?");

$stmt->bind_param("ii", $idUsers, $ModuleID, $checkPoint);
$stmt->execute();

注意 UPDATE查询中占位符的位置我不确定如何在checkPoint中获取当前项。任何帮助都会减轻女孩的压力!

0 个答案:

没有答案