2016-06-22 3 views
0

Dies wurde möglicherweise erneut gefragt, aber ich kann keine Lösung für meine "Keine Datenbank ausgewählt" Problem finden. Grundsätzlich möchte ich CSV-Inhalte auf eine Seite hochladen und sie dann in die MYSQL-Datenbank einfügen. Der folgende Code funktioniert gut, bis der Inhalt auf einer Webseite angezeigt wird. Ich kann die Details nicht in eine Datenbank bringen. Ich erhalte „keine Datenbank ausgewählt“ ... Wer mich führen durchWie CSV-Daten in eine MySQL-Datenbank in PHP eingefügt werden

<!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 { 
 
\t background: #E3F4FC; 
 
\t font: normal 14px/30px Helvetica, Arial, sans-serif; 
 
\t color: #2b2b2b; 
 
} 
 
a { 
 
\t color:#898989; 
 
\t font-size:14px; 
 
\t font-weight:bold; 
 
\t text-decoration:none; 
 
} 
 
a:hover { 
 
\t color:#CC0033; 
 
} 
 

 
h1 { 
 
\t font: bold 14px Helvetica, Arial, sans-serif; 
 
\t color: #CC0033; 
 
} 
 
h2 { 
 
\t font: bold 14px Helvetica, Arial, sans-serif; 
 
\t color: #898989; 
 
} 
 
#container { 
 
\t background: #CCC; 
 
\t margin: 100px auto; 
 
\t width: 945px; 
 
} 
 
#form \t \t \t {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'])) { 
 
\t if (is_uploaded_file($_FILES['filename']['tmp_name'])) { 
 
\t \t echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>"; 
 
\t \t echo "<h2>Displaying contents:</h2>"; 
 
\t \t readfile($_FILES['filename']['tmp_name']); 
 
\t } 
 

 
\t //Import uploaded file to Database 
 
\t $handle = fopen($_FILES['filename']['tmp_name'], "r"); 
 

 
\t while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
 
\t \t $import = "INSERT into btcdetails(mobilenumber,amount) values('$data[0]','$data[1]'),$dbname"; 
 
     
 
\t \t mysqli_query($conn,$import); 
 
\t } 
 

 
\t fclose($handle); 
 

 
\t print "Import done"; 
 

 
\t //view upload form 
 
}else { 
 

 
\t print "Upload new csv by browsing to file and clicking on Upload<br />\n"; 
 

 
\t print "<form enctype='multipart/form-data' action='uploadcsv.php' method='post'>"; 
 

 
\t print "File name to import:<br />\n"; 
 

 
\t print "<input size='50' type='file' name='filename'><br />\n"; 
 

 
\t print "<input type='submit' name='submit' value='Upload'></form>"; 
 

 
} 
 

 
?> 
 

 
</div> 
 
</div> 
 
</body> 
 
</html>

+1

Sie können die Funktionen mysql_ * und mysqli_ * nicht miteinander kombinieren. –

Antwort

0

Nun das Problem ist, dass Sie zwei der Funktionen gemischt haben. Einer ist veraltet und der andere ist in Ordnung, obwohl ich Ihnen empfehlen würde, PDO http://php.net/manual/en/book.pdo.php für sichere DB-Operationen zu verwenden.

Jetzt liegt Ihr Problem in der folgenden Code.

new mysqli($servername, $username, $password, $dbname); // Your connection 

mysql_query($import) or die(mysql_error()); // Your function 

Ändern Sie es mit dem folgenden Code.

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

Denken Sie daran, mysqli für alle Instanzen zu verwenden, wie Sie mysqli als Ihre Konnektivität verwendet haben.

+0

Sorry Ich habe versucht, den Code zu bearbeiten – Bob

+0

Ist der Fehler gleich? Wenn der DB Connectivity-Code keinen Fehler verursachte, dann denke ich, dass Sie etwas vermissen –

+0

Es ist jetzt in Ordnung @Megan. Vielen Dank – Bob

Verwandte Themen