Note that the encoding of the database tutorial should be the same as that of the txt document during import, or the garbled problem may occur.
<? Php Tutorial
// Verify the file type and error
If ($ _ FILES ["insert_first"] ["type"] = "text/plain "){
Echo "Step 1: the file type is correctly uploaded". "<br/> ";
If ($ _ FILES ["insert_first"] ["error"]) {
Echo "upload error:". $ _ FILES ["insert_first"] ["error"]. "<br/> ";
} Else {
Echo "File name:". $ _ FILES ["insert_first"] ["name"]. "<br/> ";
Echo "file type:". $ _ FILES ["insert_first"] ["type"]. "<br/> ";
Echo "file size:". ($ _ FILES ["insert_first"] ["size"]/1024). "Kb <br/> ";
Echo "temporary name:". $ _ FILES ["insert_first"] ["tmp_name"]. "<br/> ";
}
} Else {
Echo "the format of the uploaded file is incorrect ";
Exit;
}
// Connect to the database
$ Con = new mysql tutorial I ('localhost', 's503440db0', 'td9928ps ', 's503440db0 ');
If (mysqli_connect_errno ()){
Echo "failed to connect to database ";
Exit;
}
// Open the file
$ File = fopen ($ _ FILES ["insert_first"] ["tmp_name"], "r") or exit ("failed to open the file! ");
Var_dump ($ file );
Echo "<br/> ";
While (! Feof ($ file )){
// Split row by row
$ Row = explode (",", fgets ($ file ));
$ Row0 = $ row [0];
$ Row1 = (string) $ row [1];
Echo "$ row0". "---". "$ row1". "<br/> ";
// Determine whether a record exists
$ Query1 = "select firstid from firstcategory where firstid = $ row0 ";
$ Query2 = "select firstname from firstcategory where firstname = '$ row1 '";
$ Result1 = $ con-> query ($ query1 );
$ Result2 = $ con-> query ($ query2 );
// Determine the number of rows
$ Result1 = mysqli_num_rows ($ result1 );
$ Result2 = mysqli_num_rows ($ result2 );
If ($ result1 ){
Echo "the row number {$ row0} already exists <br/> ";
} Elseif ($ result2 ){
Echo "category {$ row1} already exists <br/> ";
} Else {
$ Query = "insert into firstcategory (firstid, firstname) values ($ row0, 'Music ')";
$ Result = $ con-> query ($ query );
}
}
Echo "import ended, please check if data is http://www.111cn.net correct ";
Fclose ($ file );
$ Con-> close ();
?>
Mysql database
"Create table firstcategory (
Firstid int unsigned not null primary key,
Firstname char (50) not null
)";