douminfu8033 2014-02-20 13:39
浏览 54
已采纳

我可以上传一个CSV文件,它可以通过相同的id.with PHP上传所有MySQL存在的数据

This is my code i have a upload.php and this is working fine , but i have to fill missing data of my table by uploading CSV file but its not working whenever i upload CSV its automatically generate new id and save it but my question is for example if i have a form id number is 12 now i have to insert in a phone no , this field have all information except phone no , then how can i do

id name email phone 12 Xys text@fb.com

now by CSV i have to upload only phone no

            <?php
            require_once('../web.config.php'); require_once(ROOT_PATH.'global.php');
            require_once('function.php');
            ?>
            <!DOCTYPE html>
            <title>Home</title>
            </head><body>
            <div  id="control_box" class="indexWrap">
              <div id="container">
                <div id="form">
                  <?php
            define('DB_SERVER', 'localhost');
            define('DB_USER', 'root');
            define('DB_PASSWORD', '');
            define('DB_NAME', 'upcsv');

            @$conn = mysql_connect (DB_SERVER, DB_USER, DB_PASSWORD);
            mysql_select_db (DB_NAME,$conn);
            mysql_set_charset('UTF8', $conn) or die(mysql_error());
            if(!$conn){
                die( "Sorry! There seems to be a problem connecting to our database.");
            }

            function get_file_extension($file_name) {
                return end(explode('.',$file_name));
            }

            function errors($error){
                if (!empty($error))
                {
                        $i = 0;
                        while ($i < count($error)){
                        $showError.= '<div class="msg-error">'.$error[$i].'</div>';
                        $i ++;}
                        return $showError;
                }// close if empty errors
            } // close function
            if (isset($_POST['upfile'])){
            // check feilds are not empty
            if(get_file_extension($_FILES["uploaded"]["name"])!= 'csv')
            {
            $error[] = 'Only CSV files accepted!';
            }
            if (!$error){
            $tot = 0;
            $handle = fopen($_FILES["uploaded"]["tmp_name"], "r");
            fgets($handle); 
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                for ($c=0; $c < 1; $c++) {
                        //only run if the first column if not equal to id
                        if($data[0] !='id'){
                            mysql_query("INSERT INTO tabledb(
                                                `id`,
                                                `name`,
                                                `email`,
                                                `phone`
                            )VALUES(
                                                '" . mysql_real_escape_string($data[0 ])."',
                                                '" . mysql_real_escape_string($data[1 ])."',
                                                '" . mysql_real_escape_string($data[2 ])."',
                                                '" . mysql_real_escape_string($data[3 ])."'
                                                )")or die(mysql_error());
                        }

                $tot++;}
            }
            fclose($handle);
            $content.= "<div class='success' id='message'> CSV File Imported, $tot records added </div>";

            }// end no error
            }//close if isset upfile

            $er = errors($error);
            $content.= <<<EOF
            <h3>Import CSV Data</h3>
            $er
            <form enctype="multipart/form-data" action="" method="post">
                File:<input name="uploaded" type="file" maxlength="20" /><input type="submit" name="upfile" value="Upload File">
            </form>
            EOF;
            echo $content;
            ?>
                </div>
              </div>
            </div>
            <!-- END TEMPORARY CONTENT -->
            </body>
            </html>
  • 写回答

1条回答 默认 最新

  • douludi8413 2014-02-20 14:09
    关注

    There are somethings which are unclear but I'll try to answer on the basis of information you provided.

    You mention that if form id is 20 then phone number should be added if that form id is "Prinmary key" Or even a unique index then you problem can be solved by using REPLACE query.

    Since you are using INSERT query that will always INSERT new row in a table try to user REPLACE query which will first try to find same index or primary key if it got that then it will remove/update (depending upon your table storage engine) the old row. If it delete the old row then it will insert the new row.

    REPLACE INTO tabledb(
                          `id`,
                          `name`,
                          `email`,
                          `phone`
                           )VALUES(
                            '" . mysql_real_escape_string($data[0 ])."',
                            '" . mysql_real_escape_string($data[1 ])."',
                            '" . mysql_real_escape_string($data[2 ])."',
                            '" . mysql_real_escape_string($data[3 ])."'
                           )
    

    You can use the following query..

    Feel free yo ask if you have any question.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图