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条)

报告相同问题?

悬赏问题

  • ¥15 我下载图形界面重启完就变成这样了,打字也打不了,动也动不了,该怎么解决(操作系统-centos)
  • ¥15 VBA中在窗体中遍历所有checkbox控件,提取出被选中的checkbox的caption值
  • ¥15 在Ubuntu上有什么命令,或者是系统文件能告诉我链接nvme ssd的pcie槽位是不是支持热插拔功能?
  • ¥15 ansys license许可证问题
  • ¥20 QQ号和密码都能正常登录微信 QQ号和密码登录微信显示密码错误
  • ¥15 qiankun主应用注册子应用提示跨域
  • ¥15 单片机RTOS Kernel与应用分离开发,Kernel如何调起应用?
  • ¥15 快手小店商家版APP怎么第三方APP跳转到指定用户聊天界面
  • ¥15 为什么AVL fire软件仿真时的步长改变了,仿真结果也会改变
  • ¥100 如何帮我写一个java小游戏