<?php
/**
 * @copyright	Copyright (C) Onemark IT. All rights reserved.
 * @license		onemark, see LICENSE.php
 * Petlovers! is not free web application. This version may not have been modified pursuant
 * to the Onemark License, and as not distributed it is not included or
 * is not derivative of works licensed under the Onemark License.
 * See COPYRIGHT.php for copyright notices and details.
 */
include "config/db_connect.php";
?>
<link rel="stylesheet" href="css/errorSuccess.css" type="text/css">
<div class="maincontentinner">
                <div class="content">
                  <div class="contenttitle">
                   <h2 class="form"><span>Upload Address File Booking - Import CSV File</span></h2>
                    </div><!--contenttitle-->
				    <br />
					<br/>
					<?php
		          include 'Excel/reader.php';
						if(isset($_POST['submit']))
						{
						$filename=$_FILES['files']['name'] ;
						$filename=date('U').$filename;
						$add1 = "import_data/$filename";
						
						move_uploaded_file($_FILES['files']['tmp_name'], $add1);
						chmod($add1,0777);
						basename($_FILES['files']['name'] );
						$filename1="import_data/$filename";		
				
						$data = new Spreadsheet_Excel_Reader();
						//$data->setOutputEncoding('CP1251');
						$data->setOutputEncoding('utf-8');
						$data->read($filename1);
						
						
						//columns:
						$sql = "INSERT INTO `lab_student_personal` (";
						for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++)
						{
							$sql .= "`" . mysql_escape_string($data->sheets[0]['cells'][1][$j]) . "`,";
						}
						$sql = substr($sql, 0, -1) . ") VALUES\r\n";
						
						//cells
						for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++)
						{
							$sql .= "(";
							for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++)
							{
								$sql .= "'" . mysql_escape_string($data->sheets[0]['cells'][$i][$j]) . "',";
							}
							$sql = substr($sql, 0, -1) . "),\r\n";
						}
						$sql =  substr($sql, 0, -3) . ";";
						
						$result=mysql_query($sql) or die(mysql_error());
						if($result)
						echo "<div class=messageboxok>Your  File is uploaded successfully.Thank you!</div>";
						
						
						}
							
					 //unlink($filename1);
					 unset($_POST);
					 $_POST['submit']='';
					 $_POST='';
					?>
        
					 <form action="" name ="" class="stdform stdform2"  method="post" enctype="multipart/form-data">
				    	
						<p>
                        	<label>File Name<span id="star">*</span></label>
                            <span class="field"><input  name="files" type="file" class="inp"></span>
                        </p>
                        
						 <p class="stdformbutton">
						
                        <input class="submit radius2"  name="submit" type="submit" value="Upload Data" />
					   </p>
                    </form>
                    <br clear="all">
                    
                </div><!--content-->
                
            </div>