php - 返回插入行的 id(自动递增)

标签 php mysql

  1. 我将数据插入到名为“花名册”的表中。第一列(id_roster)是使用 mysql 自动递增的 id。
  2. 我运行 SELECT 来查找 id_roster
  3. 我使用此 id_roster 将其与其他数据一起插入到表“roster_par_membre”中

    if ($insert_stmt = $mysqli->prepare("INSERT INTO `roster`(`nom_roster`, `description_roster`, `id_organisation`, `created_by`, `creation_date`,`modified_by`) VALUES (?, ?, ?, ?, ?, ?)")) {
    
        $insert_stmt->bind_param('ssiisi', $roster_name, $description_roster, $organisation_id, $user_id, $creation_date, $user_id);
    
        if (!$insert_stmt->execute()) {
                $reponse = 'Sorry, a database error occurred; please try later';
            } else {
                // if INSERT OK -> create a new line in roster_membre table
                    //1. get the roster_id
                    $sql = "SELECT r.id_roster 
                                FROM roster r
                                WHERE r.nom_roster = ?
                                LIMIT 1";
                    $stmt = $mysqli->prepare($sql);
    
                        if ($stmt) {
                            $stmt->bind_param('s', $roster_name); 
                            $stmt->execute();    // Execute the prepared query.
                            $stmt->store_result();
                            $stmt->bind_result($id_roster);
                            $stmt->fetch(); 
    
                            $level = 1;
                        //2. create a line with the roster_id and insert the membre as level 1 
                            $insert_stmt = $mysqli->prepare("INSERT INTO `roster_par_membre`(`id_membre`, `id_roster`, `level`, `modified_by`) VALUES (?,?,?,?)");
                            $insert_stmt->bind_param('iiii', $user_id, $id_roster, $level, $user_id);
                            $insert_stmt->execute();
    
                        $reponse = 'success';
                        }   
    

到目前为止,代码可以正常工作,但不是很好。 当我们在表中创建一个新行时,有没有一种方法可以直接返回一个值(带有自动增量的id)用于sql查询(将数据插入第二个表)?或者可能将两个查询(两个 INSERT)合并到一个语句中?

简短的编辑:它是一个 AJAX $response 返回值 (JSON)

最佳答案

好的,解决方法:

                //1. get the roster_id
            $sql = "SELECT r.id_roster 
                        FROM roster r
                        WHERE r.nom_roster = ?
                        LIMIT 1";
            $stmt = $mysqli->prepare($sql);

                if ($stmt) {
                    $stmt->bind_param('s', $roster_name); 
                    $stmt->execute();    // Execute the prepared query.
                    $stmt->store_result();
                    $stmt->bind_result($id_roster);
                    $stmt->fetch(); 

只需要将这部分全部替换为

$id_roster =  $mysqli->insert_id;

简单又好用。感谢 albanx

关于php - 返回插入行的 id(自动递增),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27135000/

相关文章:

php - 计算 ACF 中关系帖子的数量

PHP MySQL 插入查询不工作

MYSQL SELECT 在两个具有最小值的表上

mysql - 拉取组中特定列的最大值的mysql行

php - MySQL 存储关系(家族)树

php - 从sql数据库中删除图像

php - C PHP 扩展对象持久化

php - PDO:参数编号无效

mysql - 带连接的行总和

php - 在 MySQL 和 PHP 上使用 Left Join 对其他表中的字段求和