0

I want the user to upload a csv file into mysql database. When I run the upload.php file the only change that I see in my testcsv table,it is the table id that increases. No data are entered to the table. I use excel 2013 and I saved the file as CSV(MS-DOS). I get a bunch of errors such as the following:

Notice: Undefined index: lec_name in C:\XAMPP\htdocs\statistics\upload.php on line 48

Notice: Undefined index: dept_name in C:\XAMPP\htdocs\statistics\upload.php on line 49

enter image description here

upload.php

 <html>
 <head>
 <link rel="stylesheet" type="text/css" href="../../statistics/style.css">
 <body>
    <div class="nav">
      <ul>
        <li><a href="../../statistics/principalForm.php">Principal</a></li>
        <li><a href="../../statistics/ac_directorForm.php">Academic Director</a></li>
        <li><a href="../../statistics/lecturerForm.php">Lecturer</a></li>
        <li>
        <a href="#">Admin</a>
            <ul>
            <li><a href="../../statistics/adminFormLecturer">Save Lecturer Scores</a></li>
            <li><a href="../../statistics/adminFormServices">Save Services Scores</a></li>
            </ul>
        </li>
        <li><a href="../../statistics/logout.php">Logout</a></li>

      </ul>
  </div>

  <br />
  <br />
  <br /> 

     <div id="myform">
      <?php

       include 'connect.php';

       $deleterecords = "TRUNCATE TABLE testcsv"; //empty the table of its current records
      mysql_query($deleterecords);

       //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){

      $value1=$_POST['lec_name'];
      $value2=$_POST['dept_name'];

      $import="INSERT into testcsv(lec_name,dept_name) values('$value1','$value2')";
      mysql_query($import) or die(mysql_error());
     }

       fclose($handle);

       print "Import done";

      }
       else {

         print "Upload new csv by browsing to file and clicking on Upload<br />\n";
         print "<form enctype='multipart/form-data' action='upload.php' method='post'>";
         print "File name to import:<br /><br />\n"; 
         print "<input size='50' type='file' name='filename'><br /><br />\n";
         print "<input type='submit' name='submit' value='Upload'></form>";

       }
      ?>

      </div>
      </body>
      </head>
      </html>
5
  • In your form, there is not some code like <input type="text" name="lec_name" /> <input type="text" name="dept_name" /> Commented Apr 5, 2015 at 8:38
  • @Ashot Khanamiryan yeah i know i just want to upload a file not to input lec_name and dept_name. Commented Apr 5, 2015 at 8:40
  • Please post sample data of your csv file. Commented Apr 5, 2015 at 8:45
  • why you are not using csv column names for entering them in database? please explain Commented Apr 5, 2015 at 8:45
  • You will change this code $value1=$_POST['lec_name']; $value2=$_POST['dept_name']; with some code from csv. Can you print_r $data? I need to know what in one line in csv Commented Apr 5, 2015 at 8:48

1 Answer 1

1

Change the code

$value1=$_POST['lec_name'];
$value2=$_POST['dept_name'];

with:

   $value1=$data[0];
   $value2=$data[1];
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.