In this tutorial, We are uploading .csv ( of Excel Sheet ) in the database using PHP script. first of all need to make form in html from where we may upload the file.
after making form in html we need to setup database fields according to the need and setup the CSV file. like this image.
while inserting in to field php code firstly escape the first line from the csv sheet and start inserting data from the second line into database.
In the same page we write php code like this.
Copy and paste the upper code in same page where is html form is. then change it according to your need. we can also use some validation in this script.
if you face any difficulty for using it then contact me from contact form. I will reply you surely.
and if you want any type of blogging or Ad-sense tips then you can also contact me.
Thanks
after making form in html we need to setup database fields according to the need and setup the CSV file. like this image.
while inserting in to field php code firstly escape the first line from the csv sheet and start inserting data from the second line into database.
In the same page we write php code like this.
0) { //echo "Return Code: " . $_FILES["file"]["error"] . " "; } else { if (file_exists("upload/" . $_FILES["file"]["name"])) { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); // echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } $filexml = ""; $fname = "upload/" . $_FILES["file"]["name"]; $file = ""; $theData = ""; $qCounter = 0; $file = fopen($fname,"r") or exit(""); $theData = fgets($file); //bypass the first line.. Its a header line $a="t3"; $b="t4"; $c="t5"; $d="t6"; $e="t7"; $f="t8"; $g="t9"; $h="t10"; $i="t11"; $j="t12"; $qCount=1; //start reading the file and insert in database and create xml for psychometric test while(!feof($file)) { //$theData = fgets($file); $theData = fgetcsv($file, 512); $qCount = $qCount +1; $i = 0; $query = ""; $count = 0; $bsc_name = $theData[1];//bsc name $bsc_no = $theData[2];//bsc no. $site_name = $theData[3];//site name $site_id = $theData[4];//site ID $hub = $theData[7];//hub $node = $theData[8];//node $alarm_no = $theData[9];//alarm no. $alarm_type = $theData[10];//alarm type $alarm_desc = $theData[11];//alarm no. $severity_wise = $theData[12];//alarm no. $dateofalarm = $theData[13];//alarm no. $timeofalarm = $theData[14];//alarm no. $ext = "EXT"; $int = "INT"; if($alarm_type == $ext){ $alarm_type = 2; } else{ $alarm_type = 1; } $bsc_name = str_replace("'","\'",$bsc_name); $bsc_no = str_replace("'","\'",$bsc_no); $site_name = str_replace("'","\'",$site_name); $site_id = str_replace("'","\'",$site_id); $hub = str_replace("'","\'",$hub); $node = str_replace("'","\'",$node); $alarm_no = str_replace("'","\'",$alarm_no); $alarm_desc = str_replace("'","\'",$alarm_desc); $severity_wise = str_replace("'","\'",$severity_wise); $dateofalarm = str_replace("'","\'",$dateofalarm); $timeofalarm = str_replace("'","\'",$timeofalarm); $alarm_type = str_replace("'","\'",$alarm_type); if($a!=""){ ($fetch_row > 0) { mysql_query("insert into alarm_data(BSC_id, BCF_id, node_id, hub, alarm_id, severity_wise_id, date, time) values('$bsc_id','$BCF_id','$node_id','$hub','$alarmid','$severity_id','$dateofalarm','$timeofalarm')"); $count = $count + 1; } else { // if site not here echo "Alarm Unable to Upload Because site name -(".$site_name.") with site ID - (".$site_id.") is not available "; } } }//end of main file while loop fclose($file); // header("LOCATION: manage_alarm_data.php"); } } ?>
Copy and paste the upper code in same page where is html form is. then change it according to your need. we can also use some validation in this script.
if you face any difficulty for using it then contact me from contact form. I will reply you surely.
and if you want any type of blogging or Ad-sense tips then you can also contact me.
Thanks
0 comments :
Post a Comment