2016-04-05 17 views
0

この全体のシナリオを実際に過度に複雑にしているような気がします。うまくいけば誰かが助けることができる。PHP PDO lastinsertid of previous function

データを2つのテーブル(itemsuploads)に送信するフォームがあります。フォームデータはitemsに、アタッチメントはuploadsになります。基本的には、両方のテーブルに対応するitemIdカラムが必要です。

私の2つの機能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; 
} 

}

アドイン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> 

を両方私のテーブルの構造を、以下の通りです。

商品(の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 | 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 | 
+-----------+--------+-----+----------+------------+------+ 

(するupload_idは、プライマリユニークで自動インクリメントです)面倒なコードを許してください。私は論理を正しいものにしようとしています。それから私はそれに取り組むことができます。

アドバイスありがとうございます。

+0

あなたの関数で 'true'を返す代わりに、' $ this-> db-> lastInsertId(); 'を返して、あなたが得たものを見てください。 – Maximus2012

+0

関数に変更を加えた後、 'var_dump($ crud-> create($ inv、$ ip、$ make))'はあなたに何を与えますか? – Maximus2012

+0

これは 'create()'関数用であり、 'uploadFile()'関数用ではありません。コードごとに、 '$ itemId'は' create() '関数とは何の関係もありません。'create()'関数に 'lastInsertId()'を返すように変更した場合、 'var_dump'はテストするのに役立ちます。 – Maximus2012

答えて

0

@ Maximus2012の助けを借りて私はこれを解決できました。

私のcreate()関数を、真または偽の代わりにlastInsertId()を返すように変更しました。次に、私は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ページで、私は単純に1つのvcariableを次のように変更しました。

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

解決済み。