php - Can i insert into two different tables from the same statement? -
i have car application, cars have bunch of information avaible count kept essential on table called auto
, made huge table attributes
cool. wish can insert same array data 2 different tables under same pdo
$sql = "insert auto(year, make, model, mileage, price, vin, att1, att2, att3, att4, picture1, picture2, picture3, picture4, picture5, picture6, picture7, picture8, picture9, picture10, picture11, picture12) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
can 'and insert 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>';
i'd tempted loop on tables want run sql on;
$tables = array('auto', 'attributes'); foreach($tables $table) { $sql = 'insert '.$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 '.$table.'</h4>'; }
or create function can pass required table it;
insertvehicleinfo('auto'); insertvehicleinfo('attributes'); function insertvehicleinfo($table) { $sql = 'insert '.$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 '.$table.'</h4>'; }
Comments
Post a Comment