-1

我有一个汽车应用程序,所以汽车有一堆信息可以计算,现在我把必需品放在一张叫做桌子的桌子上auto,然后我做了一个巨大的桌子来attributes 酷。现在我希望我可以将相同的数组数据插入到同一个 pdo 下的两个不同的表中

$sql = "INSERT INTO auto(year, make, model, mileage, price, vin, att1, att2,
    att3, att4, picture1, picture2, picture3, picture4, picture5, picture6, picture7, picture8, 
    picture9, picture10, picture11, picture12) 
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

我可以做类似的事情吗'AND INSERT INTO attributes ('

$sth = $dbh->prepare($sql);
        $final = array_merge(array_values($vehicleinfo),array_values($paths));
        $sth->execute($final);
        echo '<h4 id="successMessage" style="color: red;">Vehicle added succesfully</h4>';
4

2 回答 2

2

在插入第一个表时添加触发器以将其插入第二个表。

于 2013-08-07T10:03:48.363 回答
1

我很想只遍历要在其上运行 SQL 的表;

$tables = array('auto', 'attributes');

foreach($tables as $table) {

    $sql = 'INSERT INTO '.$table.' (year, make, model, mileage, price, vin, att1, att2, att3, att4, picture1, picture2, picture3, picture4, picture5, picture6, picture7, picture8, picture9, picture10, picture11, picture12) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';

    $sth = $dbh->prepare($sql);
    $final = array_merge(array_values($vehicleinfo), array_values($paths));
    $sth->execute($final);

    echo '<h4 id="successMessage" style="color: red;">Vehicle added successfully to '.$table.'</h4>';

}

或者简单地创建一个函数,以便您可以将所需的表传递给它;

insertVehicleInfo('auto');
insertVehicleInfo('attributes');

function insertVehicleInfo($table) {

    $sql = 'INSERT INTO '.$table.' (year, make, model, mileage, price, vin, att1, att2, att3, att4, picture1, picture2, picture3, picture4, picture5, picture6, picture7, picture8, picture9, picture10, picture11, picture12) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';

    $sth = $dbh->prepare($sql);
    $final = array_merge(array_values($vehicleinfo), array_values($paths));
    $sth->execute($final);

    echo '<h4 id="successMessage" style="color: red;">Vehicle added successfully to '.$table.'</h4>';

}
于 2013-08-07T10:14:53.717 回答