2012-07-16 55 views
1

我正在將excel文件導入到我的數據庫中。導入時出現錯誤 您的SQL語法錯誤;檢查對應於您的MySQL服務器版本的手冊,以找到在'999,,,2009-02-03,9,133,2,0,0,3,1,1,1,04/02/2009,40816.0460995,'at line 1 我爲查詢做了一個回聲,它是正確的。但是當我嘗試將其插入數據庫時​​,出現此錯誤。這裏是我的數據庫結構將excel文件導入數據庫時​​出錯

CREATE TABLE `product` (
    `product_id` int(11) NOT NULL AUTO_INCREMENT, 
    `model` varchar(64) COLLATE utf8_bin NOT NULL, 
    `sku` varchar(64) COLLATE utf8_bin NOT NULL, 
    `upc` varchar(12) COLLATE utf8_bin NOT NULL, 
    `location` varchar(128) COLLATE utf8_bin NOT NULL, 
    `quantity` int(4) NOT NULL DEFAULT '0', 
    `stock_status_id` int(11) NOT NULL, 
    `image` varchar(255) COLLATE utf8_bin DEFAULT NULL, 
    `manufacturer_id` int(11) NOT NULL, 
    `shipping` tinyint(1) NOT NULL DEFAULT '1', 
    `price` decimal(15,4) NOT NULL DEFAULT '0.0000', 
    `points` int(8) NOT NULL DEFAULT '0', 
    `tax_class_id` int(11) NOT NULL, 
    `date_available` date NOT NULL, 
    `weight` decimal(15,8) NOT NULL DEFAULT '0.00000000', 
    `weight_class_id` int(11) NOT NULL DEFAULT '0', 
    `length` decimal(15,8) NOT NULL DEFAULT '0.00000000', 
    `width` decimal(15,8) NOT NULL DEFAULT '0.00000000', 
    `height` decimal(15,8) NOT NULL DEFAULT '0.00000000', 
    `length_class_id` int(11) NOT NULL DEFAULT '0', 
    `subtract` tinyint(1) NOT NULL DEFAULT '1', 
    `minimum` int(11) NOT NULL DEFAULT '1', 
    `sort_order` int(11) NOT NULL DEFAULT '0', 
    `status` tinyint(1) NOT NULL DEFAULT '0', 
    `date_added` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `date_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `viewed` int(5) NOT NULL DEFAULT '0', 
    PRIMARY KEY (`product_id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=50 ; 

和代碼導入並插入到數據庫

<?php 
require_once 'reader.php'; 
$Host = "localhost"; 
$User = "user"; 
$Password = "password"; 
$DBname = "test"; 
$Link = mysql_connect ($Host, $User, $Password) or die (mysql_error()); 
mysql_select_db($DBname); 




if(isset($_POST['submit'])) 
{ 
    $filename = $_FILES['uploadedfile']['name']; 


$TARGET_PATH = "uploads/"; 


//$filename = mysql_real_escape_string($filename); 

$TARGET_PATH .= $filename; 


if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$TARGET_PATH)) 

chmod($TARGET_PATH, 0777); 

{ 


    $data = new Spreadsheet_Excel_Reader(); 
    $data->setOutputEncoding('CP1251'); 
    $data->read($TARGET_PATH); 

$count=0; 
    for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) { 
     for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) { 
      if($j==1){ 
       $product_id=$data->sheets[0]['cells'][$i][$j]; 
        } 

        if($j==2){ 
       $model=$data->sheets[0]['cells'][$i][$j]; 
        } 

      if($j==3){ 
       $sku=$data->sheets[0]['cells'][$i][$j]; 
        } 
      if($j==4){ 
       $upc=$data->sheets[0]['cells'][$i][$j]; 
        }  

      if ($j==5){ 
       $location=$data->sheets[0]['cells'][$i][$j]; 
       } 
      if ($j==6){ 
       $quantity=$data->sheets[0]['cells'][$i][$j]; 
        }  


      if ($j==7){ 
       $stock_status_id=$data->sheets[0]['cells'][$i][$j]; 
        } 
      if ($j==8){ 
       $image=$data->sheets[0]['cells'][$i][$j]; 
       }   
       if ($j==9){ 
       $manufacturer_id=$data->sheets[0]['cells'][$i][$j]; 
       } 
       if ($j==10){ 
       $shipping=$data->sheets[0]['cells'][$i][$j]; 
        } 
        if ($j==11){ 
       $shippingamount=$data->sheets[0]['cells'][$i][$j]; 
        } 
        if ($j==12){ 
       $price=$data->sheets[0]['cells'][$i][$j]; 
        } 
       if ($j==13){ 
       $points=$data->sheets[0]['cells'][$i][$j]; 
       }  
        if ($j==14){ 
       $tax_class_id=$data->sheets[0]['cells'][$i][$j]; 
        } 
        if ($j==15){ 
       $date_available=$data->sheets[0]['cells'][$i][$j]; 
        } 
        if ($j==16){ 
       $weight=$data->sheets[0]['cells'][$i][$j]; 
        } 
        if ($j==17){ 
       $weight_class_id=$data->sheets[0]['cells'][$i][$j]; 
        } 
        if ($j==18){ 
       $length=$data->sheets[0]['cells'][$i][$j]; 
        } 
        if ($j==19){ 
       $width=$data->sheets[0]['cells'][$i][$j]; 
        } 
        if ($j==20){ 
       $height=$data->sheets[0]['cells'][$i][$j]; 
        } 
        if ($j==21){ 
       $length_class_id=$data->sheets[0]['cells'][$i][$j]; 
        } 
        if ($j==22){ 
       $subtract=$data->sheets[0]['cells'][$i][$j]; 
        } 
        if ($j==23){ 
       $minimum=$data->sheets[0]['cells'][$i][$j]; 
        } 
        if ($j==24){ 
       $sort_order=$data->sheets[0]['cells'][$i][$j]; 
        } 
        if ($j==25){ 
       $status=$data->sheets[0]['cells'][$i][$j]; 
        } 
        if ($j==26){ 
       $date_added=$data->sheets[0]['cells'][$i][$j]; 
        } 
       if ($j==27){ 
       $date_modified=$data->sheets[0]['cells'][$i][$j]; 
       } 
       if ($j==28){ 
       $viewed=$data->sheets[0]['cells'][$i][$j]; 
       }       

     } 
$count++; 

$Query72 ="insert into product (product_id,model,sku,upc,location,quantity,stock_status_id,image, manufacturer_id, shipping, shippingamount, price, points, tax_class_id, date_available, weight, weight_class_id, length, width, height, length_class_id, subtract, minimum, sort_order, status, date_added, date_modified,viewed) values('', '$model', '$sku', '$upc', '$location', $quantity, $stock_status_id, '$image', $manufacturer_id, $shipping, $shippingamount, $price, $points, $tax_class_id, $date_available, $weight, $weight_class_id, $length, $width, $height, $length_class_id, $subtract, $minimum, $sort_order, $status, $date_added, $date_modified, $viewed)"; 
echo $Query72; 
mysql_query ($Query72) or die (mysql_error()); 
if (mysql_affected_rows() >= 1) { 
    print("ROW $count - update successful!");} else {print("ROW $count - update failed!"); 
    } 



     } 
    } 
    echo "Updated Successfully"; 
} 

?> 

請幫我爲什麼我得到這個錯誤,我怎麼能解決這個問題

回答

1

使用此

$Query72 ="insert into product (product_id,model,sku,upc,location,quantity,stock_status_id,image, manufacturer_id, shipping, shippingamount, price, points, tax_class_id, date_available, weight, weight_class_id, length, width, height, length_class_id, subtract, minimum, sort_order, status, date_added, date_modified,viewed) 
values('', '".$model."', '".$sku."', '".$upc."', '".$location."', '".$quantity."','".$stock_status_id."', '".$image."', '".$manufacturer_id."', '".$shipping."', '".$shippingamount."', '".$price."', '".$points."', '".$tax_class_id."', '".$date_available."', '".$weight."', '".$weight_class_id."', '".$length."', '".$width."', '".$height."','".$length_class_id."','".$subtract."','".$minimum."','".$sort_order."', '".$status."','".$date_added."','".$date_modified."','".$viewed."')"; 

願它幫助你。

發生此錯誤是因爲您沒有正確使用引號。在您的查詢日期值中必須使用單引號。

+0

是的,我試過,但把它之後,我得到錯誤 解析錯誤:語法錯誤,在/ home/netelmbn意外T_CONSTANT_ENCAPSED_STRING /public_html/test1/import_product.php on line 194 – 2012-07-16 06:41:55

+0

可能你錯了。因爲它從我的最後工作正常。 – 2012-07-16 06:52:52

+0

@ Ajay:你說得對。我錯誤地說了。我沒有正確地檢查它,我只是評論。對不起,謝謝。它爲我工作! – 2012-07-16 07:44:31

0
雙搖籃

使用字符串變量,然後會被罰款....

相關問題