This file is/tmp/phpbeojq5.
################ You can copy this code to test ################ #
<?
Include ("include/dbclass. Inc"); // Replace the test with MySQL. Inc.
$ Q = new db_ SQL;
$ Db = $ q-> database; // define the database
Mysql_connect ('localhost: 100', $ q-> User, $ q-> password );
?>
<Form method = "Post" Action = "<? PHP echo $ php_self;?> "
Enctype = "multipart/form-Data">
<Input type = "file" name = "csv_file"
Value = "<? PHP echo $ csv_file;?> ">
Terminated by <input type = "text" name = "terminated"
Value = "," size = 2> <p>
Which table you want to insert?
<? PHP show_table ($ db, $ table);?> <P>
<Input type = "Submit" name = "Submit" value = "go">
</Form>
<? PHP
function show_field ($ db, $ table, $ num, $ selected_field) {
$ result = mysql_db_query ($ db, "Show keys from $ table ") or mysql_die ();
$ primary = "";
while ($ ROW = mysql_fetch_array ($ result ))
if ($ row ["key_name"] = "primary")
$ primary. = "$ row [column_name],";
$ result = mysql_db_query ($ db, "Show fields from $ table");
If (! $ Result) {
mysql_die ();
}< br> else {
echo " N "; while ($ ROW = mysql_fetch_array ($ result) { $ selected = ($ selected_field = $ row ["field"])? "Selected": ""; echo " ". $ row ["field"]. " N"; }< br> echo " nn";
}< BR >}
Function show_table ($ db, $ selected_table ){
$ Tables = mysql_list_tables ($ dB );
$ Num_tables = @ mysql_numrows ($ tables );
If ($ num_tables = 0 ){
Echo "no table yet ";
}
Else {
$ I = 0;
Echo "<select name = TABLE> N ";
While ($ I <$ num_tables ){
$ Table = mysql_tablename ($ tables, $ I );
$ Selected = ($ selected_table = $ table )? "Selected ":"";
Echo "<option value = $ table $ selected> $ table </option> N ";
$ I ++;
}
Echo "</SELECT> nn ";
}
}
If ($ submit ){
If (! Empty ($ csv_file) & $ csv_file! = "NONE "){
// $ Csv_file = stripslashes ($ csv_file );
$ Fp = fopen ($ csv_file, "R ");
// Show first line of the CSV file to select dababase table and its fields
Echo "<form method = post action = $ php_self> ";
$ Csv_data = fgetcsv ($ FP, 2000, "$ terminated ");
$ Num = count ($ csv_data );
For ($ C = 0; $ C <$ num; $ C ++ ){
$ Field_name [$ C] = $ csv_data [$ C];
Echo $ field_name [$ C]. "N ";
$ Table_field_name = "field $ C ";
Show_field ($ db, $ table, $ C, $ table_field_name );
Echo "<br> ";
}
// Copy the tmp file for next use
$ DEST = "/tmp/csv_file.csv ";
Copy ($ csv_file, $ DEST );
Echo "<input type = hidden name = csv_file value = $ DEST> N ".
"<Input type = hidden name = Table value = $ table> N ".
"<Input type = hidden name = terminated value = $ terminated> N ".
"<Input type = submit name = submit value = OK> </form> <p> N ";
// When Click 'OK' button, start insert data
If ($ submit = "OK "){
$ NO = 1;
While ($ csv_data = fgetcsv ($ FP, 2000, "$ terminated ")){
// $ Num = count ($ csv_data );
For ($ C = 0; $ C <$ num; $ C ++ ){
$ Table_field_name = "field $ C ";
$ First_3 = substr ($ table_field_name, 0, 3 );
$ First_2 = substr ($ table_field_name, 0, 2 );
$ Val_tmp = ($ first_3 = "FID" | $ first_3 = "int"
| $ First_2 = "ID ")?
$ Csv_data [$ C]: "'". $ csv_data [$ C]. "'";
$ Val. = $ val_tmp .",";
$ Var. = $ table_field_name .",";
}
$ Val = substr ($ Val, 0,-1 );
$ Var = substr ($ var, 0,-1 );
$ Query = "insert into $ table ($ var) values ($ Val )";
$ Val = "";
$ Var = "";
$ Q-> query ($ query );
Echo "No. $ no $ query <p> ";
$ No ++;
}
Fclose ($ FP );
}
}
}
?>