Hello everyone
I have the following code that uploads content from a CSV file to a database table. What I would like to do is extend it so that it updates two tables rather than just the one. For simplicity out of 8 columns I want 4 to go to table 1 and the other 4 to table two. My thinking here was that the array index can be utilised to achieve this but I'm not really getting it to work.
<?php
include("core/db.php");
//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) {
mysqli_query($con,"INSERT INTO site
(name, street, city, postcode)
VALUES
('$data[0]','$data[1]','$data[2]','$data[3]')");
mysqli_query($con,"INSERT INTO contacts
(firstname, lastname, gender, title)
VALUES
('$data[4]','$data[5]','$data[6]','$data[7]')");
}
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='' 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>";
}
?>
Can anyone help? Thanks