dongni8969 2016-06-22 09:05
浏览 56
已采纳

如何在PHP中将csv数据插入到mysql数据库中

This might have been asked again but I can not find a solution to my "No database selected" issue. Basically, I want to upload CSV contents to a the page and then insert them into MYSQL database. The code below does well till displaying the contents to a web page. I can not get the details into a database. I'm getting "No database selected" ...Anyone lead me through

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Upload page</title>
<style type="text/css">
body {
    background: #E3F4FC;
    font: normal 14px/30px Helvetica, Arial, sans-serif;
    color: #2b2b2b;
}
a {
    color:#898989;
    font-size:14px;
    font-weight:bold;
    text-decoration:none;
}
a:hover {
    color:#CC0033;
}

h1 {
    font: bold 14px Helvetica, Arial, sans-serif;
    color: #CC0033;
}
h2 {
    font: bold 14px Helvetica, Arial, sans-serif;
    color: #898989;
}
#container {
    background: #CCC;
    margin: 100px auto;
    width: 945px;
}
#form           {padding: 20px 150px;}
#form input     {margin-bottom: 20px;}
</style>

</head>
<body>
<div id="container">
<div id="form">

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "btccredentials";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}



//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) {
        $import = "INSERT into btcdetails(mobilenumber,amount) values('$data[0]','$data[1]'),$dbname";
      
        mysqli_query($conn,$import);
    }

    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='uploadcsv.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>
</div>
</body>
</html>

</div>
  • 写回答

1条回答 默认 最新

  • douba4275 2016-06-22 09:13
    关注

    Well the problem is you have mixed up two of the functions . One is deprecated and the other one is fine , though i would suggest you to use PDO http://php.net/manual/en/book.pdo.php for safe DB Operations.

    Now your problem lies in the below code .

    new mysqli($servername, $username, $password, $dbname); // Your connection 
    
    mysql_query($import) or die(mysql_error()); // Your function 
    

    Change it with the below code .

    mysqli_query($conn,$import) or die(mysql_error());
    

    Remember to use mysqli for all instances as you have used mysqli as your connectivity .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)