我无法找到的PHP代码中的错误:-(

时间:2012-01-15 11:27:10

标签: mysql csv

我已经制作了以下脚本来上传csv并将其导入我的MYSQL数据库,这是我的工作测试,它运行得很好。问题出现在第二个例子中,我添加了所有字段以便导入但是现在它没有做很多,它没有给出任何错误,它实际上只是在我尝试导入任何东西时将以下内容打印到页面:

编辑>>>>>>>>

有没有办法可以将下面的INSERT IGNORE消息转换为某种可用的错误消息?,因为它只是回显插入命令而不是实际显示任何类型的错误?

                        Import Successful : name
    - INSERT IGNORE hqfjt_chronoforms_data_addupdatelead (salutation, leadname, business, address, town, county, postcode, phone, email, gasoiluser,  dervuser, kerouser, annualconsgasoil, annualconsderv, annualconskero, gasoilmargin, dervmargin, keromargin, fueldetails, lubesdetails, otherdetails, sentletterday, sentlettermonth, sentletteryear, sentpostcardday, sentpostcardmonth, sentpostcardyear, sentquoteday, sentquotemonth, sentquoteyear, lastvisitedday, lastvisitedmonth, lastvisitedyear, receivegasoilmailinglist, receivedervmailinglist, receivekeromailinglist, gasoilmailinglist, dervmailinglist, keromailinglist, mailtype, createdby) VALUES ('salutation','name','business name','address','town','county','postcode','phone','email','gasoil user','derv user','kero user','annual consumption gas oil','annual consumption derv','annual consumption kero','gas oil margin','derv margin','kero margin','fuel details','lubes details','other details','sent letter day','sent letter month','sent letter year','sent postcard day','sent postcard month','sent postcard year','sent quote day','sent quote month','sent quote year','last visited day','last visited month','last visited year','mailtype','receive gasoil mailing list frequency','receive derv mailing list frequency','receivekero mailing list frequency','gasoil mailing list','derv mailinglist','kero mailinglist')

这是我用来检查上传和导入功能是否正常工作的初始测试代码:

    <?php
    //database connect info here

    //check for file upload
    if(isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])){

        //upload directory
        $upload_dir = "./csv";

        //create file name
        $file_path = $upload_dir . $_FILES['csv_file']['name'];

        //move uploaded file to upload dir
        if (!move_uploaded_file($_FILES['csv_file']['tmp_name'], $file_path)) {

            //error moving upload file
            echo "Error moving file upload";

        }

        //open the csv file for reading
        $handle = fopen($file_path, 'r');


        while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {

            //Access field data in $data array ex.
            $leadname = $data[0];
            $postcode = $data[1];
            $phone = $data[2];
            echo 'Import Successful : ';
            echo $leadname;
            echo '<br/>';

            //Use data to insert into db
            $sql = sprintf("INSERT IGNORE hqfjt_chronoforms_data_addupdatelead (leadname, postcode, phone) VALUES ('%s','%s','%s')",
                        mysql_real_escape_string($leadname),
                        mysql_real_escape_string($postcode),
                        mysql_real_escape_string($phone)
                        );
            mysql_query($sql) or (mysql_query("ROLLBACK") and die(mysql_error() . " - $sql"));
        }


        //delete csv file
        unlink($file_path);
    }
    ?>

然后我继续添加所有字段.......现在由于某种原因它不能导入,只是在页面顶部打印消息。我知道我可能在某个地方错过了一些东西,但我找不到它的生命。

    <?php
    //database connect info here

    //check for file upload
    if(isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])){

        //upload directory
        $upload_dir = "./csv";

        //create file name
        $file_path = $upload_dir . $_FILES['csv_file']['name'];

        //move uploaded file to upload dir
        if (!move_uploaded_file($_FILES['csv_file']['tmp_name'], $file_path)) {

            //error moving upload file
            echo "Error moving file upload";

        }

        //open the csv file for reading
        $handle = fopen($file_path, 'r');


        while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {

            //Access field data in $data array ex.
                        $salutation = $data[0];
                        $leadname = $data[1];                           
                        $business = $data[2];
                        $address = $data[3];
                        $town = $data[4];
                        $county = $data[5];
                        $postcode = $data[6];
                        $phone = $data[7];                          
                        $email = $data[8];
                        $gasoiluser = $data[9];
                        $dervuser = $data[10];
                        $kerouser = $data[11];
                        $annualconsgasoil = $data[12];
                        $annualconsderv = $data[13];
                        $annualconskero = $data[14];
                        $gasoilmargin = $data[15];
                        $dervmargin = $data[16];
                        $keromargin = $data[17];
                        $fueldetails = $data[18];   
                        $lubesdetails = $data[19];  
                        $otherdetails = $data[20];                          
                        $sentletterday = $data[21];
                        $sentlettermonth = $data[22];
                        $sentletteryear = $data[23];
                        $sentpostcardday = $data[24];
                        $sentpostcardmonth = $data[25];
                        $sentpostcardyear = $data[26];                          
                        $sentquoteday = $data[27];
                        $sentquotemonth = $data[28];
                        $sentquoteyear = $data[29];
                        $lastvisitedday = $data[30];
                        $lastvisitedmonth = $data[31];
                        $lastvisitedyear = $data[32];
                        $receivegasoilmailinglist = $data[33];
                        $receivedervmailinglist = $data[34];
                        $receivekeromailinglist = $data[35];
                        $gasoilmailinglist = $data[36];
                        $dervmailinglist = $data[37];
                        $keromailinglist = $data[38];   
                        $mailtype = $data[39];                      
                        $createdby = $data[40];

            echo 'Import Successful : ';
            echo $leadname;
            echo '<br/>';

            //Use data to insert into db
            $sql = sprintf("INSERT IGNORE hqfjt_chronoforms_data_addupdatelead (salutation, leadname, business, address, town, county, postcode, phone, email, gasoiluser, dervuser, kerouser, annualconsgasoil, annualconsderv, annualconskero, gasoilmargin, dervmargin, keromargin, fueldetails, lubesdetails, otherdetails, sentletterday, sentlettermonth, sentletteryear, sentpostcardday, sentpostcardmonth, sentpostcardyear, sentquoteday, sentquotemonth, sentquoteyear, lastvisitedday, lastvisitedmonth, lastvisitedyear, receivegasoilmailinglist, receivedervmailinglist, receivekeromailinglist, gasoilmailinglist, dervmailinglist, keromailinglist, mailtype, createdby) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
                        mysql_real_escape_string($salutation),
                        mysql_real_escape_string($leadname),                            
                        mysql_real_escape_string($business),
                        mysql_real_escape_string($address),
                        mysql_real_escape_string($town),
                        mysql_real_escape_string($county),
                        mysql_real_escape_string($postcode),
                        mysql_real_escape_string($phone),                           
                        mysql_real_escape_string($email),
                        mysql_real_escape_string($gasoiluser),
                        mysql_real_escape_string($dervuser),
                        mysql_real_escape_string($kerouser),
                        mysql_real_escape_string($annualconsgasoil),
                        mysql_real_escape_string($annualconsderv),
                        mysql_real_escape_string($annualconskero),
                        mysql_real_escape_string($gasoilmargin),
                        mysql_real_escape_string($dervmargin),
                        mysql_real_escape_string($keromargin),
                        mysql_real_escape_string($fueldetails), 
                        mysql_real_escape_string($lubesdetails),    
                        mysql_real_escape_string($otherdetails),                            
                        mysql_real_escape_string($sentletterday),
                        mysql_real_escape_string($sentlettermonth),
                        mysql_real_escape_string($sentletteryear),
                        mysql_real_escape_string($sentpostcardday),
                        mysql_real_escape_string($sentpostcardmonth),
                        mysql_real_escape_string($sentpostcardyear),                            
                        mysql_real_escape_string($sentquoteday),
                        mysql_real_escape_string($sentquotemonth),
                        mysql_real_escape_string($sentquoteyear),
                        mysql_real_escape_string($lastvisitedday),
                        mysql_real_escape_string($lastvisitedmonth),
                        mysql_real_escape_string($lastvisitedyear),
                        mysql_real_escape_string($mailtype),
                        mysql_real_escape_string($receivegasoilmailinglist),
                        mysql_real_escape_string($receivedervmailinglist),
                        mysql_real_escape_string($receivekeromailinglist),
                        mysql_real_escape_string($gasoilmailinglist),
                        mysql_real_escape_string($dervmailinglist),
                        mysql_real_escape_string($keromailinglist),                         
                        mysql_real_escape_string($createdby)
                        );
            mysql_query($sql) or (mysql_query("ROLLBACK") and die(mysql_error() . " - $sql"));
        }


        //delete csv file
        unlink($file_path);
    }
    ?>

1 个答案:

答案 0 :(得分:0)

这在PHP中不起作用:

mysql_query($sql) or (mysql_query("ROLLBACK") and die(mysql_error() . " - $sql"));

两个问题:

  • 你不要这样用“和”;反而创建一个控制结构。

  • 您正在运行ROLLBACK命令,然后查询SQL错误 - 这可能会覆盖mysql_error()的值。

尝试此操作以进行错误处理:

$error = mysql_query($sql);

if (!$query) 
 { 
   echo mysql_error() . " - $sql";
   mysql_query("ROLLBACK"); 
   die();
 }