How to display loaded csv file data using php

I am currently making a project in php to upload a CSV file and insert data into a database. Before inserting data, I need to display the data in table format.

I used this code to load and insert data.

if (isset($_POST['submit'])) 
{
$name = $_POST['camname'];
if (is_uploaded_file($_FILES['filename']['tmp_name'])) 
{
    echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
}

Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
    $import="INSERT into uploadmail(name,email) values('$name','$data[0]')";
    mysql_query($import) or die(mysql_error());
}

fclose($handle);
}

      

This encoding successfully inserts data into the database, but I need to display the data in a table format. Please give any suggestions.

THANKS IN THE FRAMEWORK.

+3


source to share


1 answer


Just replace your code with this. The data will be saved to the database and will be displayed as well. I have added the mapping data to the table code in your code.



if (isset($_POST['submit'])) 
{
$name = $_POST['camname'];
if (is_uploaded_file($_FILES['filename']['tmp_name'])) 
{
    echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
}

Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");
echo "<table>\n<tr><th>Name</th><th>Email</th></tr>";
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
    echo "<tr>";
    $import="INSERT into uploadmail(name,email) values('$name','$data[0]')";

    echo "<td>".$name."</td><td>".$data[0]."</td>";
    mysql_query($import) or die(mysql_error());
    echo "</tr>";
}
echo "\n</table>";
fclose($handle);
}

      

+4


source







All Articles