Jump to content

Insert csv into mysql


remmingtonshowdown

Recommended Posts

Yeah I searched the intertubes long and far and I was able to piece together my code, alas it only inserts the second line of the csv and then stops... I've searched and searched and I'm just getting worn out... can anyone point out what's missing or help me figure it out- I'm not asking for someone else to do it- I'd like to figure it out but I'm at a loss...

 

Here's the code:

//log into the database
$username = "ica";
$password = 'sesame';
mysql_connect("localhost", $username, $password) or die ("Unable to connect to server");
mysql_select_db("ica");

//get the file from the form
$file =  $_FILES['results']['name'];

//open and begin the csv processing
$handle = fopen($file, "r");
fgetcsv($handle, 993, ","); //ignore first header;

//Zikula actions
//create var $timestamp with current time
$timestamp = date('Y-m-d H:i:s');

//create var $userid and get the user's id: Zikula AIP?
$userid = "2"; //currently set to admin but might be better set to anon?

//create var $exitstingPubs and get total number of existing pubs from pagesetter_counters for given tid X: pagesetter_getPubCount plugin
$pubCount = "SELECT `pg_count` FROM `zk_pagesetter_counters` WHERE `pg_name` = 'tid1'";

$existingPubsRaw = mysql_query($pubCount);
$row = mysql_fetch_row($existingPubsRaw);
$existingPubs = $row[0];
$totalPubs = $row[0];

while (($data = fgetcsv($handle, ",")) !== FALSE)
{
//replace below with proper data to reflect pagesetter pub fields and csv fields etc...
$organization = str_replace(",", "", $data[0]);
$eventYear = str_replace(",", "", $data[1]);
$eventPermit = str_replace(",", "", $data[2]);
$raceDate = str_replace(",", "", $data[3]);
$discipline= str_replace(",", "", $data[4]);
$category= str_replace(",", "", $data[5]);
$gender= str_replace(",", "", $data[6]);
$class= str_replace(",", "", $data[7]);
$age= str_replace(",", "", $data[8]);
$license= str_replace(",", "", $data[9]);
$lastName= str_replace(",", "", $data[10]);
$firstName= str_replace(",", "", $data[11]);
$bib= str_replace(",", "", $data[12]);
$teamName= str_replace(",", "", $data[13]);
$time= str_replace(",", "", $data[14]);
$place= str_replace(",", "", $data[15]);

$totalPubs = $totalPubs + 1;

//put the data from the csv intot he table including extra pagesetter fields.  fields will be replaced with pg_field(n)...build accordingly$
$import ="INSERT INTO zk_pagesetter_pubdata1  values('$totalPubs','$totalPubs','approved','1','0','1','-1','1','1','admin','2','$timestamp','$timestamp','$timestamp','NULL','x_all','$organization','$eventYear','$eventPermit','$raceDate','$discipline','$category','$gender','$class','$age','$license','$lastName','$firstName','$bib','$teamName','$time','$place')";

//echo $serial."<br>";
mysql_query($import) or die(mysql_error());
echo 'result X inserted into database';

//($insertedPubs = $insertedPubs ++) 
$insertedPubs = $insertedPubs + 1;

//($existingPubs = $existingPubs ++) 
$existingPubs = $existingPubs + 1;

//for each item inserted into the db insert a new line into pagesetter_pubheader with the data: "pg_tid (X), pg_pid ($existingPubs), pg_hitcount (0), pg_onlineid ($existingPubs), pg_deleted (0)"
$pubheader = "INSERT INTO zk_pagesetter_pubheader values('1','$existingPubs','0','$existingPubs','0')";
mysql_query($pubheader)  or die(mysql_error());

//for each line inserted into the db insert a new line into pagesetter_revisions with the data: "pg_tid (X), pg_id (1), pg_pid ($existingPubs), pg_prevversion (0), pg_user ($userid), pg_timestamp ($timestamp)"
$revisions = "INSERT INTO zk_pagesetter_revisions values('1','$existingPubs','$existingPubs','0','2','$timestamp')";
mysql_query($revisions)  or die(mysql_error());

$data++;
}

//actions preformed after all the results have been stored in the database
//insert $insertedPubs into pagesetter_counter for the given tid X
$counters = "UPDATE zk_pagesetter_counters SET pg_count = $existingPubs WHERE pg_name = 'tid1'";
mysql_query($counters)  or die(mysql_error());
echo 'total number:'.$existingPubs.' of instances updated with';

fclose($handle);

Link to comment
Share on other sites

Here is what I use:

 

if (isset($_POST['importFile'])){


$target_path = "";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    $thefile=basename( $_FILES['uploadedfile']['name']);
} else{
    echo "There was an error uploading the file, please try again!";
}



include ("../con.php");

  $fcontents = file ("$thefile");  
  
  # expects the csv file to be in the same dir as this script 
  
  for($i=0; $i<sizeof($fcontents); $i++) {  
  
      $line = trim($fcontents[$i]);  
      
      $arr = explode("\t", $line);  
      
      $sql = "insert into TABLE values (' ', '". substr(implode("''", $arr),1) ."')";  
                  
      mysql_query($sql); 
      
      if(!mysql_error()) { 
      header("location: import_form.php"); 
      }
      
  elseif(mysql_error()){
  echo "IMPORT FAILED";
  }  
}

mysql_query("INSERT INTO uploaded (id, name, stamp) VALUES ('', '$thefile', '$DATE_TIME_STAMP')");

mysql_close($con);

$myFile = "$thefile";
unlink($myFile);

}  // END IF SUBMIT

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.