0

I need to create a form in php and mysql in which it will create a table on a default set database for example "ramgest" and i want it to be only one text field with table name and one file upload which will get the imported .csv into the newly created table. Table structure

id, cod, den_material, greutate_totala, cant_reper, lg, cod_furnizor where id to be primary key with auto increment

Here is my current upload code:

   <html>
<?php include("header.php"); ?>
<?php include("config.php"); ?>
   <body style="
    background-color: rgb(128, 151, 185);
">

<form action="import_file.php" method="post"
        enctype="multipart/form-data">
<table>
    <tr>
        <td>
            Comanda:
        </td>
        <td>
            <input type="file" name="file" id="file">
        </td>
    </tr>
    <tr>
        <td colspan="2" align="right">
            <input type="submit" name="submit" value="Submit">
        </td>
    </tr>
</table>
</form>

</body>
</html>

Which is processed by import_file.php

<?php
if ($_FILES["file"]["error"] > 0)
{
    echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 10240) . " Kb<br>";
    //echo "Stored in: " . $_FILES["file"]["tmp_name"];
    $a=$_FILES["file"]["tmp_name"];
    //echo $a;

    $connect = mysql_connect('localhost','root','');
if (!$connect) {
die('Could not connect to MySQL: ' . mysql_error());
}   
//your database name
$cid =mysql_select_db('test',$connect);

// path where your CSV file is located
//define('CSV_PATH','C:/xampp/htdocs/');
//<!-- C:\xampp\htdocs -->
// Name of your CSV file
$csv_file = $a; 

if (($getfile = fopen($csv_file, "r")) !== FALSE) {
         $data = fgetcsv($getfile, 1000, ",");
   while (($data = fgetcsv($getfile, 1000, ",")) !== FALSE) {
     //$num = count($data);
       //echo $num;
        //for ($c=0; $c < $num; $c++) {
            $result = $data;
            $str = implode(",", $result);
            $slice = explode(",", $str);

            $col1 = $slice[0];
            $col2 = $slice[1];
            $col3 = $slice[2];
             $col4 = $slice[3];
             $col5 = $slice[4];
             $col6 = $slice[5];
             $col7 = $slice[6];



$query = "INSERT INTO comanda(id, cod, den_material ,greutate_totala ,cant_reper ,lg ,cod_furnizor) VALUES('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."','".$col7."')";
$s=mysql_query($query, $connect );
}
}
echo "<script>alert('Record successfully uploaded.');window.location.href='list.php?id=1';</script>";
//echo "File data successfully imported to database!!";
mysql_close($connect);
}
?>

Need help ASAP :)

2
Contributors
1
Reply
25
Views
2 Years
Discussion Span
Last Post by urtrivedi
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.