无法将csv文件导入mysql

时间:2015-07-02 05:33:24

标签: php mysql xampp

plzz帮我修复以下代码中的错误.. 它无法读取PHP代码。我该怎么做?我还想在关闭时显示连接的状态。

<table width="600" style="margin:115px auto; background:#f8f8f8; border:1px solid #eee; padding:20px 0 25px 0;">
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data">

<tr><td colspan="2" style="font:bold 15px arial; text-align:center; padding:0 0 5px 0;">Browse and Import your excel File </td></tr>
<tr>
<td width="50%" style="font:bold 12px tahoma, arial, sans-serif; text-align:right; border-bottom:1px solid #eee; padding:5px 10px 5px 0px; border-right:1px solid #eee;">Select file</td>
<td width="50%" style="border-bottom:1px solid #eee; padding:5px;"><input type="file" name="file" id="file" /></td>
</tr>
<tr>
<td style="font:bold 12px tahoma, arial, sans-serif; text-align:right; padding:5px 10px 5px 0px; border-right:1px solid #eee;">Import</td>
<td width="50%" style=" padding:5px;"><input type="submit" value="submit" /></td>
</tr>
</form>
</table>


<?php 
if(isset($_POST['submit']))
{   
echo connection_status();
$connect = mysql_connect('localhost:8080','root','');

if (!$connect) {
 die('Could not connect to MySQL: ' . mysql_error());
 }
$cid =mysql_select_db('ts_order_track',$connect);

$file = $_FILES['file']['tmp_name'];

if (($getfile = fopen($file, "r")) != FALSE) {
echo "csvs open";

        $data = fgetcsv($getfile, 3000, ",");
        echo "csvs Data Reading";
        while (($data = fgetcsv($getfile, 3000, ",")) != FALSE) {
         $num = count($data); 
         for ($c=0; $c < $num; $c++) {
             $result = $data; 
             $str = implode(",", $result); 
             $slice = explode(",", $str);
             $col1 = $slice[0]; 
             $col2 = $slice[1];
             $col3 = $slice[2]; 
             $col4 = $slice[3];
             $col5 = $slice[4];
             $col6 = $slice[5];
             $col7 = $slice[6];
             $col8 = $slice[7];
             $col9 = $slice[8];
             $col10 = $slice[9];
             $col11 = $slice[10];
             $col12 = $slice[11];
             $col13 = $slice[12];
            $col14 = $slice[13];
            $col15 = $slice[14];
            $col16 = $slice[15];
            $col17 = $slice[16];
            $col18 = $slice[17];
            $col19 = $slice[18];
            $col20 = $slice[19];
            $col21 = $slice[20];
            $col22 = $slice[21];
            $col23 = $slice[22];
            $col24 = $slice[23];
            $col25 = $slice[24];
            $col26 = $slice[25];
            $col27 = $slice[26];
            $col28 = $slice[27];
            $col29 = $slice[28];
            $col30 = $slice[29];
            $col31 = $slice[30];
            $col32 = $slice[31];
            $col33 = $slice[32];
            $col34 = $slice[33];    
            $col35 = $slice[34];        
            $col36 = $slice[35];        
            $col37 = $slice[36];             



$sql = "INSERT INTO tsd_orders(order_id,    customer_id,    firstname,  lastname,   email,  telephone,  mobile, shipping_firstname, shipping_lastname,  shipping_mobile,    shipping_address_1, shipping_address_2, shipping_city,  shipping_zone,  shipping_postcode,  shipping_country,   shipping_method,    payment_method, payment_code,   weight, date_added, date_modified,  import_order_id,    sub_total,  shipping_charges,   tax_charges,    total,  order_status,   courier,    awb_code,   coupon_amount,  coupon_code,    product_name,   product_sku,    product_model,  product_quantity,   product_price)
VALUES('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."','".$col7."','".$col8."','".$col9."','".$col10."','".$col11."','".$col12."','".$col13."','".$col14."','".$col15."','".$col16."','".$col17."','".$col18."','".$col19."','".$col20."','".$col21."','".$col22."','".$col23."','".$col24."','".$col25."','".$col26."','".$col27."','".$col28."','".$col29."','".$col30."','".$col31."','".$col32."','".$col33."','".$col34."','".$col35."','".$col36."','".$col37."')";

$s=mysql_query($sql, $connect ); 
echo "csvs Data Reading";
   } 
  }
echo "File data successfully imported to database!!"; 
mysql_close ( $connect );
echo connection_status();
}}
?>

它给出了以下错误: ** 警告:mysql_connect():MySQL服务器在第22行的C:\ xampp \ htdocs \ trendspry.php消失了

警告:mysql_connect():读取问候数据包时出错。第22行的C:\ xampp \ htdocs \ trendspry.php中的PID = 1824

警告:mysql_connect():MySQL服务器在第22行的C:\ xampp \ htdocs \ trendspry.php消失了

致命错误:第22行的C:\ xampp \ htdocs \ trendspry.php超过了30秒的最长执行时间**

我使用的是xampp ..其中server是localhost,端口是8080.感谢您的帮助。

1 个答案:

答案 0 :(得分:-1)

你在这里缺少名字属性

<input type="submit" value="submit" />

将其更改为

<input type="submit" value="submit" name="submit"/>
还有一件事。不要使用mysql_ *,因为它们已被弃用。阅读Here

<强>已更新

试试这个

$connect = mysql_connect('localhost:8080','root','');

将其更改为

$connect = mysql_connect('localhost','root','');
相关问题