我在三个不同的实例中插入有关汽车的信息,在第一个汽车中,它只包含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
答案 0 :(得分:1)
假设您正在使用PDO,请调用$dbh->lastInsertId()
以获取上次插入操作的自动增量ID。