Link to home
Start Free TrialLog in
Avatar of vidya v
vidya v

asked on

i have csv file in that i have image field but i dont know to insert the image to database which is in csv file

if ($_FILES['bulk']['size'] > 0 ) {
            
                    $file = fopen($filename, "r");
                  if($file)
                  {
                        echo "opend";
                  }
                  else
                  {
                        echo "not";
                  }
            
              while (($get = fgetcsv($file, 10000, ","))!==false)
               {
$class=$get[0];
$first=$get[1];
$last=$get[2];
$gender=$get[3];
$dob=$get[4];
$add=$get[5];
$city=$get[6];
$state=$get[7];
$zip=$get[8];
$mob=$get[9];
$alt=$get[10];
$email=$get[11];
$rfid=$get[12];
 $file = $get[13];
 $file_name = $file[0];
 $file_tmp_name = $file[1];
 move_uploaded_file($file_tmp_name,"../upload/".basename($file_name));
if($i>0)
{
                 $sql = mysql_query("INSERT into student$school (class,firstname,lastname,gender,dob,address,city,state,zip,mobilenum,alternate,email,rfid,image)
                     values ('$class','$first','$last','$gender','$dob','$add','$city','$state','$zip','$mob','$alt','$email','$rfid','$file')") or die(mysql_error());
      
}
$i++;      
               }
                  if(!$sql)
                        {
                        echo "<script>alert('some thing went wrong')</script>";      
                        }
                        else {
                                echo "<script>alert('successfully Imported.')</script>";
                        }
                    
             }
      }
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

you probably got your image path using:

$imgpath = "../upload/".basename($file_name);

and then use this variable in your insert statement which specified the image field name.

got the idea?
Where does $filename come from
 $file = fopen($filename, "r");

Open in new window

What are you expecting these lines to do? $file is a file handle but you are treating them as an array
 $file_name = $file[0];
 $file_tmp_name = $file[1];

Open in new window

You seem to be expecting an uploaded file - but you never use it (that we can see)
if ($_FILES['bulk']['size'] > 0 )

Open in new window


Not really possible to tell from your code what it is you are
a) Trying to do
b) what your question is
You seem to have quite a bit wrong with your code. The logic seems a little 'off' and you're also trying to use the old, deprecated mysql methods. You need to switch this out to mysqli or PDO. Take a look at this to get an idea of how to upload a CSV File and insert the contents into the DB using PDO.

if (isset($_FILES['bulk']) && $_FILES['bulk']['size'] > 0): // check to see if we have a file

    if (($file = fopen($_FILES['bulk']['tmp_name'], "r")) !== FALSE): // open the file

        $dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password); // connect to the DB
        $stmt = $dbh->prepare("INSERT INTO yourTable (val1, val2, val3, val4) VALUES (?, ?, ?, ?)"); // prepare the query

        while (($data = fgetcsv($file, 1000, ",")) !== FALSE): // loop through the CSV file

            $stmt->execute($data); // execute the query

        endwhile;

        move_uploaded_file($_FILES['bulk']['tmp_name'], "./upload/" . $_FILES['bulk']['name']);

    endif;

endif;

Open in new window

You'll ned to edit it to match your own data structure. It also assumes that the data in the CSV matches the table structure and is in the correct field order, but it should give you some idea on the right direction :)
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.