Import csv database using PHP. Follow the following code and you will be done with CSV import script using PHP. If you have any questions you can feel free comment we will reply you ASAP.
PHP Code
<?php if(isset($_POST["Import"])) { $filename=$_FILES["file"]["tmp_name"]; if($_FILES["file"]["size"] > 0) { $file = fopen($filename, "r"); mysql_query('truncate table table'); while (($data = fgetcsv($file, 10000, ",")) !== FALSE) { $sql = "INSERT into table(name,email,avg,sr,runs,wickets,hs,sixes,fours,longsixes,created) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]', now())"; mysql_query($sql); } fclose($file); echo "CSV File has been successfully Imported"; } else echo "Invalid File:Please Upload CSV File"; } ?>
HTML Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Import CSV/Excel file</title> </head> <body> <form enctype="multipart/form-data" method="post"> <table border="1" width="40%" align="center"> <tr > <td colspan="2" align="center"><strong>Import CSV</strong></td> </tr> <tr> <td align="center">CSV/Excel File:</td> <td><input type="file" name="file" id="file"></td> </tr> <tr > <td colspan="2" align="center"><input type="submit" name="Import" value="Import"></td> </tr> </table> </form> </body> </html>
You might be wondering why I have given the pre tag brush: js as classes. You need to mention the syntax highlighting that needs to be used to highlight your code. You can find the full list of supported syntax declarations [here](http://alexgorbatchev.com/SyntaxHighlighter/manual/brushes/).