php - 如何在没有 "bind_param"的情况下进行更新

标签 php mysql mysqli

所以我正在尝试为我的公司设置一个简单的出勤网络应用程序。 checkin / checkout 是通过一个按钮完成的,为了做到这一点,我几乎同时进行了插入和更新。 但是要进行适当的更新,我需要一个“bind_param”的“id”,我有点卡在那里。我敢打赌这是一个简单的问题,但我找不到解决方法。

if ($pre == 0) {
    $sql = $mysqli->prepare("INSERT INTO presence(code_pr,heure_debut,user_id)
        VALUES( ? , ? , ? )
        ") or die(mysqli_error($mysqli));
    $sql->bind_param("sss", $code_pr, $heure_debut, $idcompte);

    if (!$sql->execute()) {
        die('<h3 style="color:red;" align="center">ERREUR</h3>' . mysqli_error($mysqli));
    }
} else {
    if ($pre == 1 && $precheck == 1) {
        $sqlsrt = $mysqli->prepare("UPDATE presence SET heure_fin=? WHERE user_id=? AND id=?") or die(mysqli_error($mysqli));
        $sqlsrt->bind_param('ss', $heure_fin, $idcompte);
        if (!$sqlsrt->execute()) {
            die('<h3 style="color:red;" align="center">ERREUR</h3>' . mysqli_error($mysqli));
        }
    }
}

我希望能够有一个 id 放入我的“bind_param”中。

最佳答案

我认为“$conn->lastInsertId()”会有所帮助。

if (!$sql->execute()) {
    die('<h3 style="color:red;" align="center">ERREUR</h3>' . 
    mysqli_error($mysqli));
}else{
   //Here you can take the ID of the last Insert
   $id = $conn->lastInsertId()
}

关于php - 如何在没有 "bind_param"的情况下进行更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56887618/

相关文章:

php - SQLSTATE[HY000] [1045] 用户 mimi_oboss 访问被拒绝

php - 如何在 ng-repeat 中使用 ng-class 为每个迭代 div 设置类

C# 和 MySQL - 语法不正确?

php - 将值从数据库插入到 xml 表,并在 html 中使用 ajax 输出它们

php - mysqli php搜索表单空白

php - Mysqli 使用准备好的语句获取数组

php - 使用phpMyAdmin在php中将用户添加到MySQL数据库

php - Apache2 网站在整个站点中导航时不会更改默认 URL

PHP 为从数据库中选择的多个用户发送邮件

javascript - 使用 Ajax 在提交表单时保持在同一页面上不起作用