我插入了关于汽车的信息,在三种不同的情况下,第一种情况下它进入auto,只包含4行基本年份的品牌型号和价格,并且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
假设您使用的是PDO,则调用$dbh->lastInsertId()
从上次插入操作中获取自动递增id。