0

我在三种不同的情况下插入有关汽车的信息,首先它转到自动,其中仅包含 4 行基本年份制造型号和价格,并且唯一的 ID 由 mysql 自动设置。伟大的。现在我如何返回为该实例设置的 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

4

1 回答 1

1

假设您正在使用 PDO,请调用$dbh->lastInsertId()以从上次插入操作中获取自动增量 id。

于 2013-08-16T02:57:35.517 回答