douji1058 2016-04-05 15:27
浏览 19

PHP PDO lastinsertid的上一个函数

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.

  • 写回答

1条回答 默认 最新

  • donglv7097 2016-04-06 09:02
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法