donglu4633 2015-08-18 05:42
浏览 27
已采纳

CSV到MYSQL PHP

While submitting file i get this error-

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Name,Short Description,Description,Address,Phone,Email,Category) VALUES(,,,,,,)' at line 1)

I Tried everything but still my code not working perfectly. Below is my php code-

<div id="form">        
    <?php
    $connect = mysql_connect("mysql6.000webhost.com","username","password");
    mysql_select_db("a7611052_123",$connect); //select the table 
    //Connect to Database
    $deleterecords = "TRUNCATE TABLE tablename";
    //empty the table of its current records
    mysql_query($deleterecords);
    //Upload File
    if (isset($_POST['submit'])) {
    if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
    echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
    echo "<h2>Displaying contents:</h2>";
    readfile($_FILES['filename']['tmp_name']);
    }

    //Import uploaded file to Database
    $handle = fopen($_FILES['filename']['tmp_name'], "r");
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $Bn=$data[0];
    $Sd=$data[1];
    $D=$data[2];
    $A=$data[3];
    $P=$data[4];
    $E=$data[5];
    $C=$data[6];
    $import="INSERT INTO BusinessD(Business Name,Short Description,Description,Address,Phone,Email,Category) VALUES($Bn,$Sd,$D,$A,$P,$E,$C)";
    mysql_query($import) or die(mysql_error());
    }
    fclose($handle);
    print "Import done";
    //view upload form
    }else {
    print "Upload new csv by browsing to file and clicking on Upload<br />
";
    print "<form enctype='multipart/form-data' action='B.php' method='post'>";
    print "File name to import:<br />
";
    print "<input size='50' type='file' name='filename'><br />
";      
    print "<input type='submit' name='submit' value='Upload'></form>";
    }
    ?>
    </div> 
  • 写回答

2条回答 默认 最新

  • dqgo99177 2015-08-18 05:49
    关注

    Write your Insert query as

    INSERT INTO BusinessD(`Business Name`,`Short Description`,`Description`,`Address`,`Phone`,`Email`,`Category`) VALUES('$Bn','$Sd','$D','$A','$P','$E','$C')"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答