If you need to populate a database with multiple records, instead of creating each record individually, you can create a MySQL query in your word processor and then create all the records and populate all the fields at once.
Here’s how:
Create a new PHP document and load the viewer_functions.php with this code at the top of your page (don’t forget to change the server path information), or copy the code block from one of your other viewers.
<?php header('Content-type: text/html; charset=utf-8'); ?> <?php /require_once "/path_to_your/cmsAdmin/lib/viewer_functions.php";
Then for adding values to a single field use this format:
$query = "INSERT INTO cms_table_name (field_1) VALUES
(‘field_1_value1'), (‘field_1_value2’), (‘field_1_value3’), (‘field_1_value4’)”; mysql_query($query) or die("MySQL Error: " .mysql_error(). " with query $query\n");
?>
If you wanted to populate 2 fields at the same time, you’d use:
$query = "INSERT INTO cms_table_name (field_1,field_2) VALUES
(‘field_1_value1',‘field_2_value1'), (‘field_1_value2’,‘field_2_value2'), (‘field_1_value3’,‘field_2_value3'), (‘field_1_value4’,‘field_2_value4')”; mysql_query($query) or die("MySQL Error: " .mysql_error(). " with query $query\n");
?>
NOTE 1: There is no comma after the last set of values. NOTE 2: Unless you’ve changed the default prefix for your table names, the correct cms_table_name format in the MySQL query for a table called “articles” would be cms_articles NOTE 3: If you execute the query more than once, you'll insert more than one set of records into your table.
EXAMPLES:
To Insert individual records for all 50 United State's State names (plus the District of Columbia) into a table called "states" with fields for State and Country you can use the following MySQL query:
$query = "INSERT INTO cms_states (state,country) VALUES
('Alabama','UNITED STATES'), ('Alaska','UNITED STATES'), ('Arizona','UNITED STATES'), ('Arkansas','UNITED STATES'), ('California','UNITED STATES'), ('Colorado','UNITED STATES'), ('Connecticut','UNITED STATES'), ('Delaware','UNITED STATES'), ('District of Columbia','UNITED STATES'), ('Florida','UNITED STATES'), ('Georgia','UNITED STATES'), ('Guam','UNITED STATES'), ('Hawaii','UNITED STATES'), ('Idaho','UNITED STATES'), ('Illinois','UNITED STATES'), ('Indiana','UNITED STATES'), ('Iowa','UNITED STATES'), ('Kansas','UNITED STATES'), ('Kentucky','UNITED STATES'), ('Louisiana','UNITED STATES'), ('Maine','UNITED STATES'), ('Maryland','UNITED STATES'), ('Massachusetts','UNITED STATES'), ('Michigan','UNITED STATES'), ('Minnesota','UNITED STATES'), ('Mississippi','UNITED STATES'), ('Missouri','UNITED STATES'), ('Montana','UNITED STATES'), ('Nebraska','UNITED STATES'), ('Nevada','UNITED STATES'), ('New Hampshire','UNITED STATES'), ('New Jersey','UNITED STATES'), ('New Mexico','UNITED STATES'), ('New York','UNITED STATES'), ('North Carolina','UNITED STATES'), ('North Dakota','UNITED STATES'), ('Ohio','UNITED STATES'), ('Oklahoma','UNITED STATES'), ('Oregon','UNITED STATES'), ('Pennsylvania','UNITED STATES'), ('Rhode Island','UNITED STATES'), ('South Carolina','UNITED STATES'), ('South Dakota','UNITED STATES'), ('Tennessee','UNITED STATES'), ('Texas','UNITED STATES'), ('Utah','UNITED STATES'), ('Vermont','UNITED STATES'), ('Virginia','UNITED STATES'), ('Washington','UNITED STATES'), ('West Virginia','UNITED STATES'), ('Wisconsin','UNITED STATES'), ('Wyoming','UNITED STATES')";
mysql_query($query) or die("MySQL Error: " .mysql_error(). " with query $query\n"); ?>
|