PHP PDO 上一个函数的lastinsertid

标签 php mysql pdo

我觉得我真的把整个场景变得过于复杂化了。希望有人能帮忙。

我有一个将数据提交到两个表(itemsuploads)的表单。表单数据转到items,附件转到uploads。基本上我希望两个表都有相应的 itemId 列。

我的两个函数create()uploadFile()都可以工作。但是,我不确定如何在名为 $itemId 的变量中使用 $crud->create() 的 lastInsertId 值 - 请参阅我的代码中的注释。

我的函数的简化版本如下,包括注释。

class.crud.php

class crud {

private $db;

function __construct($DB_con) {
    $this->db = $DB_con;
}

public function create($inv, $ip, $make){
    $stmt = $this->db->prepare("INSERT INTO items (inv,ip,make) VALUES (:inv,:ip,:make");
    $stmt->bindparam(":inv", $inv);
    $stmt->bindparam(":ip", $ip);
    $stmt->bindparam(":make", $make);
    $stmt->execute();
    return true;
}

public function uploadFile($itemId, $inv, $file, $file_type, $file_size) {
    $stmt = $this->db->prepare("INSERT INTO uploads (itemId,inv,file,type,size) VALUES (:itemId,:inv,:file,:file_type,:file_size)");
    $stmt->bindParam(":itemId", $itemId); // inserts 777
    $stmt->bindParam(":inv", $inv);
    $stmt->bindparam(":file", $file);
    $stmt->bindparam(":file_type", $file_type);
    $stmt->bindparam(":file_size", $file_size);
    $stmt->execute();  
    return true;
}

}

add-data.php

if (isset($_POST['btn-save'])) {
    $itemId = '777'; //this successfully inserts 777 into the uploads.itemId teble, but i'd like to insert the lastInsertId value of $crud->create()
    $inv = $_POST['inv'];
    $ip = $_POST['ip'];
    $make = $_POST['make'];
    $file = rand(1000, 100000) . "-" . $_FILES['file']['name'];
    $file_loc = $_FILES['file']['tmp_name'];
    $file_size = $_FILES['file']['size'];
    $file_type = $_FILES['file']['type'];
    $folder = "uploaded_files/";

    if ($crud->create($inv, $ip, $make)) {
        echo 'success';
    } else {
        echo 'error';;
    }

    if (move_uploaded_file($file_loc, $folder . $file)) {
            $crud->uploadFile($itemId, $inv, $file, $file_type, $file_size);
        }
}

<form method='post' enctype="multipart/form-data">
    <input type='text' name='inv'>
    <input type='text' name='ip'>
    <input type='text' name='make'>
    <input type='file' name='file'>
    <button type="submit" name="btn-save"></button>
</form>

我的两个表的结构如下;

items (itemId 是主要的、唯一的且自动增量)

+--------+---------+-----------------+-------+
| itemId | inv     | ip              | make  |
+--------+---------+-----------------+-------+
| 1      | 1293876 | 123.123.123.123 | Dell  |
+--------+---------+-----------------+-------+
| 2      | 4563456 | 234.234.234.234 | Dell  |
+--------+---------+-----------------+-------+
| 3      | 7867657 | 345.345.345.345 | Apple |
+--------+---------+-----------------+-------+

项目 (upload_id 是主要的、唯一的且自动递增)

+-----------+--------+-----+----------+------------+------+
| upload_id | itemId | inv | file     | type       | size |
+-----------+--------+-----+----------+------------+------+
| 56        | 777    | 123 | test.txt | text/plain | 266  |
+-----------+--------+-----+----------+------------+------+
| 57        | 777    | 123 | test.txt | text/plain | 266  |
+-----------+--------+-----+----------+------------+------+
| 58        | 777    | 123 | test.txt | text/plain | 266  |
+-----------+--------+-----+----------+------------+------+

请原谅凌乱的代码。我只是想让逻辑正确,然后我就可以继续工作。

如有任何建议,我们将不胜感激。

最佳答案

因此,在@Maximus2012的帮助下,我能够解决这个问题。

我更改了 create() 函数以返回 lastInsertId() 来代替 true 或 false。然后,我将 create() 函数返回的值分配给变量,而不是使用静态值。

所以我的工作代码现在看起来像这样;

public function create($inv, $ip, $make){
    $stmt = $this->db->prepare("INSERT INTO items (inv,ip,make) VALUES (:inv,:ip,:make");
    $stmt->bindparam(":inv", $inv);
    $stmt->bindparam(":ip", $ip);
    $stmt->bindparam(":make", $make);
    $stmt->execute();
    return $this->db->lastInsertId();
}

然后在我的 add-data.php 页面中,我只需将一个 vcariable 更改为以下内容;

$itemId = $crud->create($inv, $ip, $make);

已解决。

关于PHP PDO 上一个函数的lastinsertid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36430691/

相关文章:

php - HTML 中的复选框不起作用。单击复选框时没有任何反应。如何让它发挥作用?

java - War文件无法在Tomcat 7上部署

Mysql多重连接语法错误

php - 使用 PDO 语句更新 mysql 时遇到问题

使用 PDO 的 PHP sql

php - 带有数组接口(interface)的集合上的 array_map?

php - 从mysql的2个表中取出数据并显示它们

mysql - 为一组中的每个选择最后 n 个日期记录

mysql - 在不遵守单词顺序的情况下使用 REGEXP 进行查询

php - ZF2 中的 CORS POST 请求变为 OPTIONS 请求