简体   繁体   中英

PHP PDO lastinsertid of previous function

I feel like I'm really overly complicating this whole scenario. Hopefully somebody can help.

I have a form which submits data to two tables ( items and uploads ). The form data goes to items , and the attachment to uploads . Basically I'd like both tables to have a corresponding itemId column.

My two functions create() and uploadFile() both work. However, I'm not sure how to use the the lastInsertId value of $crud->create() in my variable named $itemId - see comments in my code.

Reduced versions of my functions are below, including comments.

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>

The structure of both my tables are as follows;

items (itemId is primary, unique and auto-increment)

+--------+---------+-----------------+-------+
| 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 |
+--------+---------+-----------------+-------+

items (upload_id is primary, unique and auto-increment)

+-----------+--------+-----+----------+------------+------+
| 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  |
+-----------+--------+-----+----------+------------+------+

Please forgive the messy code. I'm just trying to get the logic correct and then I can work on it.

Any advice is appreciated.

So with help from @Maximus2012 I was able to solve this.

I changed my create() function to return lastInsertId() in place of just true or false. Then, I assigned the value returned by the create() function to a variable rather than using a static value.

So my working code now looks like this;

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();
}

Then in my add-data.php page I simply changed one vcariable to the following;

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

Solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM