Turn OICQ data into MySQL data
Last Update:2017-02-28
Source: Internet
Author: User
This file is/tmp/phpbeojq5.
################# can copy this code to test #################
?
Include ("Include/dbclass.inc"); You can use MYSQL. INC class instead of testing
$q = new Db_sql;
$db = $q->database; Define the database
mysql_connect (' localhost:3306 ', $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 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 ();
}
else {
echo "<select name=field$num>n";
while ($row = Mysql_fetch_array ($result)) {
$selected = ($selected _field = = $row ["field"])? "Selected": "";
echo "<option value=". $row [Field]. "$selected >".
$row [Field]. "</option>n";
}
echo "</select>nn";
}
}
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 a of the CSV file to select Dababase table and its fields
echo "<form method=post action= $PHP _self>";
$csv _data = Fgetcsv ($fp, Watts, "$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, Watts, "$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);
}
}
}
?>