$closingHour = '23:59:59'; } if (empty($slots)) { $slots = -1; } $db = connect(); try { // using a transaction to preserve the database integrity $db->beginTransaction(); insertSegment($db, $name, $slots, $lng, $lat, $city); // the key of the newly added segment is needed as a foreign key for the following queries $id = $db->lastInsertId(); // we need to create a default name if the name wasn't filled in the form if (empty($name)) { $name = 'Parking #' . $id; updateName($db, $id, $name); } insertEntranceAdress($db, $entrance, $id); insertWalkingAdress($db, $walking, $id); insertExitAdress($db, $exit, $id); insertOpeningHours($db, $openingHour, $closingHour, $id); insertPaymentType($db, $payment, $id); insertParkLot($db, $name, $city); insertFeatures($db, $id, $features); $db->commit(); } catch (PDOExecption $e) { $db->rollBack(); print "Error!: " . $e->getMessage() . "</br>"; } /** * Inserts a new segment with specified informations in the database
//add the updatedNameColumn $field_array = array(); $fields = mysql_list_fields($db, $country['short_name']); $columns = mysql_num_fields($fields); for ($i = 0; $i < $columns; $i++) { $field_array[] = mysql_field_name($fields, $i); } echo '<br />'; var_dump($field_array); echo '<br />'; if (!in_array('updatedName', $field_array)) { echo "altering table: adding updatedName<br/>"; $sqlAddUpdatedColumnToTable = "ALTER TABLE `" . $country['short_name'] . "` ADD `updatedName` TINYINT NOT NULL DEFAULT '0';"; mysql_query($sqlAddUpdatedColumnToTable) or die('died modifying the table to add the updatedName column: <br />sql: ' . $sqlAddUpdatedColumnToTable . ' <br />error: ' . mysql_error()); } else { echo "table was already altered <br />"; } $sqlGetCities = "SELECT id, name FROM `" . $country['short_name'] . "` WHERE updatedName != '1';"; $resultCities = mysql_query($sqlGetCities) or die('died getting cities: <br />sql: ' . $sqlGetCities . ' <br />error: ' . mysql_error()); while ($city = mysql_fetch_assoc($resultCities)) { updateName($city['id'], $city['name'], $country['short_name']); } } } } function updateName($id = null, $cityName = null, $countryCode = null) { $city = addslashes(filter_var($cityName, FILTER_SANITIZE_STRING)); $sqlUpdateCityName = "UPDATE `" . $countryCode . "` SET name = '" . $city . "', updatedName = '1' WHERE id ='" . $id . "'"; mysql_query($sqlUpdateCityName) or die('died updating city name: <br />sql: ' . $sqlUpdateCityName . ' <br />error: ' . mysql_error()); }