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.

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

报告相同问题?

悬赏问题

  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?