Jump to content

CSV Imports


01hanstu

Recommended Posts

Hi, I have a booking system incorporate into our intranet. The problem is that we have to manually time the names of the teachers in the fields then the class names. My idea is to utilise the MIS Server we have and do an export of the room timetables and then when we open the manage timetable page we can click on import csv the point to where it is then it will put the data onto the form the we can just click submit.

 

Is this possible?

 

Thanks

Stuart

Link to comment
Share on other sites

Hi, Thanks again, I Guess i could simply rename the csv file to suit when its emailed to me. and copy it to the web directory the run the script.

 

Are you able to show me how, using a fixed name, to fill the textfield from a csv and if you can show me one i can try to replicate it to do them all (Bit of copy N paste)

 

Thanks

- Stuart

Link to comment
Share on other sites

okay I've commented a script to help show you how to use it:

 

<?php
//include db connection;

//error function
function errors($error){
if (!empty($error))
{
	$i = 0;
	$showError.= "<p><span class=\"error\" id=\"message\">";
	while ($i < count($error)){
	$showError.= $error[$i].'<br />';
	$i ++;}
	$showError.= "</span></p>";
	echo $showError;
}// close if empty errors

} // close function

//function to detect file extension
function get_file_extension($file_name) {
return end(explode('.',$file_name));
}



if (isset($_POST['upfile'])){
// check feilds are not empty


//if file is not a csv generate an error
if(get_file_extension($_FILES["uploaded"]["name"])!= 'csv')
{
$error[] = 'Only CSV files accepted!';
}

if (!$error){
// $_FILES['uploaded']['tmp_name'];

$tot = 1;
$handle = fopen($_FILES["uploaded"]["tmp_name"], "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
for ($c=0; $c < 1; $c++) {       
//echo "<pre>".print_r($data)."</pre>";


//import into databsse
$sql = mysql_query("INSERT INTO table(
col1,
col2,
col3,
col4
)VALUES(
'$data[0]',
'$data[1]',
'$data[2]',
'$data[3]',
'$data[4]'
)")or die(mysql_error());			
$tot++;

}
}
fclose($handle);
echo  "CSV File Imported, $tot records added";
  
}// end no error
}//close if isset upfile

//show any errors
errors($error);

//show upload form
echo "<form enctype=\"multipart/form-data\" action=\"\" method=\"post\">
File:<input name=\"uploaded\" type=\"file\" maxlength=\"20\" /><input type=\"submit\" name=\"upfil\e" value=\"Upload File\">
</form>";
?>

 

if your getting the file emailed to you the filename can be anything since you can upload it through a form.

 

there is a function called errors which will collect any errors from an array also another function to determine the extension if the file being uploaded.

 

You'll need to chnage the database table name and the column names each column data comes back as an array $data[] put the column section number inside the array like $data[$1] which gets the second segment as arrays start at 0

 

you should be able to see what I mean from the script.

 

Hope this is clear.

Link to comment
Share on other sites

Hi,

This is looking Promising, but i have an error and cant get rid of it  :confused:

 

My Connect.php is as follows:

 

<?php

$hostname = "xx.xx.xx.xx";

$database = "db_name";

$username = "u_name";

$password = "p_word";

$conn= mysql_connect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);

?>

 

But i get an error saying "No database selected"

 

Can you help

 

Thanks

 

Stuart

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.