2013-08-16 57 views
0

我在三個不同的實例中插入有關汽車的信息,首先它將自動包含僅包含4行的基本年份製造型號和價格,並自動設置唯一ID由mysql。大。現在我該如何返回爲該實例設置的ID,以便我可以使用它將其插入圖片和屬性的其他兩個實例?插入表格並返回自動增量ID分配

$toauto = "INSERT INTO auto(year, make, model, mileage, price) 
     VALUES (?, ?, ?, ?, ?)"; 

/*  
     //sql for inserting into auto 
     $toattributes = "INSERT INTO attributes(auto_id, 
     bodystyle, enginesize, cyl, hp, fuel, transmission, shifts, od, cd, mp3, dvd, 
     gps, sound_system, sradio, tachometer, clock, trip, eweather, digitalboard, rwd, 
     fwd, awd, fxf, cruisecontrol, tiltsteering, ac, removabletop, keyless, airbags, 
     alloy, trunkantitrap, ewindows, emirrors, eseat, elocks, antitheft, leadheadlights 
     ) 
     VALUES ($autoid, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 
     ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 
     //sql to insert into the pictures 
     $topictures = "INSERT INTO auto(year, make, model, mileage, price) 
     VALUES (?, ?, ?, ?, ?)"; 
*/  
     print_r($auto); 
     print_r($paths); 
     //$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     //inserting to auto 
     $sth = $dbh->prepare($toauto); 
     $final = array_merge(array_values($auto)); 
     $sth->execute($final); 
/* 
     //inserting to attributes 
     $sth = $dbh->prepare($toauto); 
     $final = array_merge(array_values($auto)); 
     $sth->execute($final); 
     //inserting to pictures 
     $sth = $dbh->prepare($toauto); 
     $final = array_merge(array_values($auto)); 
     $sth->execute($final); 
*/ 

行名稱是auto_id

回答

1

假設你正在使用PDO,調用$dbh->lastInsertId()從上次插入操作得到自動遞增ID。

+0

先生我可以做這個'$ autoid = $ dbh-> lastInsertId()''? –

+0

是的,這正是你需要做的。 PDO :: lastInsertId()返回最後一次插入時生成的ID。 – adear11