This question has been asked but no answers yet...
Have a csv upload form to load into an existing MySQL table for use on a Drupal page, hence everything being in one php. This is what I have (thanks coyotelab):
$mysqli = new mysqli($server, $user, $password, $database);
if ($mysqli->connect_error) {
die("Failed to connect: " . $mysqli->connect_error);
}
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
echo "<h2>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h2>";
}
$handle = fopen($_FILES['filename']['tmp_name'], "r");
if ($handle !== FALSE) {
$import="LOAD DATA LOCAL INFILE '" . $_FILES['filename']['tmp_name'] . "' INTO TABLE Demo2 CHARACTER SET 'utf8' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' IGNORE 1 LINES;";
mysql_query($import) or die('<p style="color:red;">Database import unsuccessful: ' . mysql_error() . '</p>');
}
fclose($handle);
print "<p style='color:blue;'>Import done</p>";
echo "<p style='position: relative; top: 10px;'><strong>Displaying contents:</strong></p>";
readfile($_FILES['filename']['tmp_name']);
unset($_POST);
} else {
print "<p>Import new csv</p>\n";
print "<form enctype='multipart/form-data' action='?q=drupal-relative-path-to/upload.php' method='post'>\n";
print "<input size='50' type='file' name='filename'><br />\n";
print "<input type='submit' name='submit' value='Import'></form>";
At first, the form was test locally and granting all permissions to localhost worked and data imported successfully. But on a Drupal server, there will be no change to localhost permissions. Other forms to insert SQL data have worked fine from Drupal pages. Only happens with the upload file function.
So far the form shows up on Drupal, but when uploading and submitting a CSV, I get the same error as testing on localhost without permissions. Error is "No database selected"
My thought was to change the location where uploaded temp files live, though haven't figured that out or might not be possible on the Drupal server. Tried adding a second db connect line, but that doesn't work. Must have something to do with the temp directory.
This form will be on a private network.