0

I have problem to import csv file to mysql and I'm not familiar in this topic,here is the code that I want to use to import csv file to mysql ,I dont have a idea why it still give a error .I need someone to help me check what is wrong in my coding here.

import.php

    <script>
       $(document).ready(function() { 

                $('#submit').live('click', function(){ 
                  $("#imageform").submit();

                });
            }); 




            </script>

    </head>
     <body>
     <div id="imp1">
     <div id="import">Importing into Current Server</div>
     <form id="imageform" method="post" enctype="multipart/form-data" action="validate.php">
     <div class="far">
        <label id="sel">Upload File</label>
            <div class="far1">
            <div id="preview"></div>
    <input type="file" name="filename" id="filename" />
    </div>
    <div class="clear"></div>
    </div>
    <div><a href="#"><input id="submit" type="submit" name="submit" value="submit" /><span></span></a></div></form>
    </div>  
   </body>

mysql.php

<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "sample";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
?>

validate.php

     include "mysql.php"; //Connect to Database

        $deleterecords = "TRUNCATE TABLE customer";
        mysql_query($deleterecords);

        //Upload File
        if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
        {
             $fname = $_FILES['filename']['name'];

             $chk_ext = explode(".",$fname);

             if(strtolower($chk_ext[1]) == "csv")
             {

                 $filename = $_FILES['filename']['name'];
                 $handle = fopen($filename, "r");

                 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
                 {
                    $sql = "INSERT into customer(item2,item3,item4,item5,item6,item7,item8,item9,item10,item11) 
    values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]')";
//item1 is AI
                    mysql_query($sql) or die(mysql_error());
                 } 

                 fclose($handle);
                 echo "Successfully Imported";
             }
             else
             {
                 echo "Invalid File";
             }    
        }


        ?>

Thanks for your help .

4
  • 1
    what error does it give? Commented Oct 1, 2013 at 3:30
  • Server error.The website encountered an error while retrieving localhost/import/import/validate.php. It may be down for maintenance or configured incorrectly. << When I check the table the value in item1 that I set as auto increment is increasing.I'm get so confused now Commented Oct 1, 2013 at 3:37
  • 1
    that is a very generic error - e.g. it can be caused by duplicate function names, mismatched quotes, lack of ; etc - look in your log files and see what the actual error was. Commented Oct 1, 2013 at 3:39
  • @ZackNewsham I already found the problem ,got same ['name'] in $fname = $_FILES['filename']['name']; -at line 9 with $filename = $_FILES['filename']['name']; -at line 16 . please forward back your previous comment so I can mark as answered :) Commented Oct 1, 2013 at 3:49

1 Answer 1

1

it can be caused by duplicate function names, mismatched quotes, lack of ; etc.

In this case the problem was solved by replacing $fname = $_FILES['filename']['name']; -at line 9 with $filename = $_FILES['filename']['name']; at line 16

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.