search

duDaoRuConfig.php页面

<?php$db_name="wordpress";//如需更改数据库配置在此更改即可$conn = mysql_connect("localhost", "root", "root");mysql_select_db($db_name, $conn);mysql_query("set names 'UTF-8'");//解析csv文件,返回二维数组,第一维是一共有多少行csv数据,第二维是键名为csv列名,值为当前行当前列的csv数据值function input_csv($csv_file) {    $result_arr = array ();    $i = 0;    while ($data_line = fgetcsv($csv_file, 10000)) {        if($i == 0){            $GLOBALS['csv_key_name_arr'] = $data_line;            $i++;            continue;        }        foreach($GLOBALS['csv_key_name_arr'] as $csv_key_num=>$csv_key_name){            $result_arr[$i][$csv_key_name] = $data_line[$csv_key_num];        }        $i++;    }    return $result_arr;}?><script type="text/javascript" src="jquery-1.8.2.js"></script>

doDaoRu.php

<form action="doDaoRu2.php" method="post" enctype="multipart/form-data">    <input type="file" name="csv_file" size="50" maxlength="100000" /><br/>    <input type="submit" value="submit"/></form>

doDaoRu2.php

<?phpinclude_once("duDaoRuConfig.php");$dir = "./upload/";if (is_dir($dir) == false) {    mkdir($dir, 0777);//在页面目录下要新建upload文件夹用来保存上传csv文件}//1,存储csv文件$csv_filename = $_FILES["csv_file"]["name"];move_uploaded_file($_FILES["csv_file"]["tmp_name"], "./upload/" . $_FILES["csv_file"]["name"]);//2,获取所有表名$selAllTableName_str = "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = '$db_name'";$allTableName_que = mysql_query($selAllTableName_str);//3,获取csv文件数据的所有列名$csv_key_name_arr = array();//4,以csv列名为键名获取csv所有数据$csv_file = fopen('upload/'.$csv_filename, 'r');$result_arr = input_csv($csv_file);fclose($csv_file);?><form action="doDaoRu3.php" method="post">    列名:    <select class="table_name_sel" name="table_name_sel">        <option> </option>        <?php        while($tableName_row = mysql_fetch_array($allTableName_que)){//可选择所有表名        ?>        <option><?php echo $tableName_row['table_name']?></option>        <?php        }        ?>    </select>    <br/>    <br/>    <br/>    <?php    foreach($csv_key_name_arr as $csv_key_name)//罗列csv所有列名,并选择要导入到的对应表名,或不导入该csv列    {    ?>    <span>        <input type="hidden" name="<?php echo $csv_key_name;?>" class="csv_key_name_hid" value=""/><?php echo $csv_key_name;?>        <select class='table_column_name_sel'>            <option> </option>        </select>    </span>    <?php    }    ?>    <input type="hidden" name="csv_filename_hid" value="<?php echo $csv_filename;?>"/>    <input type="submit" value="submit"/></form><script type="text/javascript">$(".table_column_name_sel").change(function(){//当为csv列名选择对应表列名时,为该csv隐藏域值赋选择的表列名    $(this).parent().find("input").val($(this).val());})$(".table_name_sel").change(function(){    $(".csv_key_name_hid").val("");    var tableName = $(this).val();    var ajaxAddUrl = "doDaoRuAjax.php";    //window.location = ajaxAddUrl+"?tableName="+tableName;    $.post(ajaxAddUrl,{'tableName':tableName},function(jieShou){        $(".table_column_name_sel option").remove();        $(".table_column_name_sel").append("<option> </option>");        $.each(jieShou,function(i,n){            $(".table_column_name_sel").append("<option>"+n+"</option>");        })    },"json");});</script>

doDaoRu3.php

<?phpinclude_once("duDaoRuConfig.php");$csv_filename = $_POST['csv_filename_hid'];$tableName = $_POST['table_name_sel'];//保存需要保存的csv数据的列与表列的关联$table_real_column_name_arr = array();//3,保存csv文件数据的所有列名$csv_key_name_arr = array();//获取csv所有列名及解析数据$csv_file = fopen('upload/'.$csv_filename, 'r');$result_arr = input_csv($csv_file);//csv数据条数(已扣除第一行列名)$result_arr_len = count($result_arr);//将需要保存的csv列与表的列关联起来foreach($csv_key_name_arr as $csv_key_name){    if($_POST[$csv_key_name]){//判断前页该csv列已赋表列名        $table_real_column_name_arr[$csv_key_name] = $_POST[$csv_key_name];    }}//使用批插入,拼凑所有需插入的数据到一个长字符串的sql语句中$all_insert_data_value_str = "";for ($i = 1; $i <= $result_arr_len; $i++) { //循环获取各字段值    $csv_line_data_value = "";    $j = 1;    foreach($table_real_column_name_arr as $csv_real_key_name=>$table_real_column_name){        if($j == count($table_real_column_name_arr)){            $csv_line_data_value .= " ' ".$result_arr[$i][$csv_real_key_name]."' ";        }else{            $csv_line_data_value .= " '".$result_arr[$i][$csv_real_key_name]."', ";        }        $j++;    }    $all_insert_data_value_str .= " ($csv_line_data_value) ,";}$all_insert_data_value_str = substr($all_insert_data_value_str,0,-1); //去掉最后一个逗号//拼凑所有需插入值的字段名到一个长字符串的sql语句中$all_insert_column_name_str = "";$i = 1;foreach($table_real_column_name_arr as $csv_real_key_name=>$table_real_column_name){    if($i == count($table_real_column_name_arr)){        $all_insert_column_name_str .= " $table_real_column_name ";    }else{        $all_insert_column_name_str .= " $table_real_column_name , ";    }    $i++;}//问题,1文本中如有回车换行会导致字符串中有转义字符导入失败,2中文乱码//执行批插入,csv导入完成$query = mysql_query("insert into $tableName ($all_insert_column_name_str) values $all_insert_data_value_str");//批量插入数据表中fclose($csv_file);if($query){    echo '导入成功!';}else{    echo '导入失败!';}?>

doDaoRuAjax.php

<?phpinclude_once("duDaoRuConfig.php");$talbeName = $_POST['tableName'];//查询该表所有列$selAllColumns_str = "SHOW COLUMNS FROM $talbeName";$allColumnsName_que = mysql_query($selAllColumns_str);$allColumnsName_arr = array();$i = 0;while($allColumnsName_row = mysql_fetch_array($allColumnsName_que)){    $allColumnsName_arr[$i] = $allColumnsName_row['Field'];    $i++;}ob_end_clean();echo json_encode($allColumnsName_arr);?>

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment