可能会再次询问,但我找不到我的"没有选择数据库的解决方案"问题。基本上,我想将CSV内容上传到页面,然后将它们插入到MYSQL数据库中。以下代码在将内容显示到网页之前一直很好。我无法将详细信息存入数据库。我得到"没有选择数据库" ......任何人都引导我完成
<!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 />\n";
print "<form enctype='multipart/form-data' action='uploadcsv.php' method='post'>";
print "File name to import:<br />\n";
print "<input size='50' type='file' name='filename'><br />\n";
print "<input type='submit' name='submit' value='Upload'></form>";
}
?>
</div>
</div>
</body>
</html>
&#13;
答案 0 :(得分:0)
问题是你混淆了两个功能。一个是弃用的,另一个是好的,但我建议你使用PDO http://php.net/manual/en/book.pdo.php进行安全的数据库操作。
现在您的问题出在下面的代码中。
new mysqli($servername, $username, $password, $dbname); // Your connection
mysql_query($import) or die(mysql_error()); // Your function
使用以下代码更改它。
mysqli_query($conn,$import) or die(mysql_error());
请记住,对于所有实例都使用mysqli
,因为您已使用mysqli
作为连接。