Jump to content

CSV importing... Can't figure out why this doesn't work now...


ChrisMayhew

Recommended Posts

I need someone to look over this and just see if they can see why this won't work anymore?

 

It worked when the site was in development but when it came down to the day it's not working.

 

Basically it reads a CSV and inserted the driver into a Quali or Results table in the correct order but it's not working anymore?

 

If someone could have a read over it and suggest what might be going wrong that would be great.

 

Edit: It basically just added the same driver 24 times in the table.

 

Public Function ProcessUpload($UR, $RaceID, $RaceName) {
Global $MDB;
// Configuration - Your Options
  $allowed_filetypes = array('.csv'); // These will be the types of file that will pass the validation.
  $max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
  $upload_path = './resultsfiles/'; // The place the files will be uploaded to (currently a 'files' directory).

$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

If (strpos($filename, "Quali") == True) {
$tbl_name = "QualiPositions";
$FN = " - Qualifying";
} Else {
$tbl_name = "RacePositions";
$FN = " - Race";
}

$SQL = "SELECT count(*) FROM $tbl_name WHERE RaceID = $RaceID";
$Count = $MDB->prepare($SQL);
$Count->execute();
$Count = $Count->fetchColumn();

If ($Count == 24) {
$Msg = "<div class='error'>Error: We already have 24 Results for this Session.</div>";
}

// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
  $Msg = "<div class='error'>Error: The file you attempted to upload is not allowed.</div>";

// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
  $Msg ="<div class='error'>Error: The file you attempted to upload is too large.</div>";

// Check if we can upload to the specified path, if not DIE and inform the user.
if(!is_writable($upload_path))
  $Msg ="<div class='error'>Error: You cannot upload to the specified directory, please CHMOD it to 777.</div>";

If (strpos($Msg, "Error:") == False) {

// Upload the file to your specified path.
$filename = str_replace(" ", "", $RaceName)."$FN.csv";
if (move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)) {
    $FilePath = $upload_path . $filename;
	if (($handle = fopen($FilePath, "r")) !== FALSE) {
		$Pos = 0;
	    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
	        $num = count($data);
	        
	        for ($c=0; $c < $num; $c++) {
	        $Pos++;
	        	If ($Pos <= 24) {
		            $Driver = explode(" ", $data[$c]);
		            $FName = $Driver[0];
		            $LName = $Driver[1]." ".$Driver[2]." ".$Driver[3];
			            $STH = $MDB->query("SELECT * FROM Drivers WHERE FirstName = '$FName' AND LastName = '$LName'");
						$STH->setFetchMode(PDO::FETCH_OBJ);
						while($row = $STH->fetch()) {
						$DriverID = $row->DriverID;
						$ChassisID = $row->ChassisID;
						$EngineID = $row->EngineID;
						}
					$STH = $MDB->prepare("INSERT INTO $tbl_name(RaceID, DriverID, ChassisID, EngineID, Position, DSQ) VALUES (:RID, :DID, :ChaID, :EngID, :Pos, :DSQ)");
			 		$STH->execute(array('RID' => $RaceID, 'DID' => $DriverID, 'ChaID' => $ChassisID, 'EngID'=>$EngineID, 'Pos'=>$Pos, 'DSQ' => 0));
			 		$Msg = "<div class='team_confirm'>The Results have been uploaded for the $RaceName</div>";
		 		} Else {
		 		If ($tbl_name == "RacePositions") {
			 		$Driver = explode(" ", $data[$c]);
			            $FName = $Driver[0];
			            $LName = $Driver[1]." ".$Driver[2]." ".$Driver[3];
			            $STH = $MDB->query("SELECT * FROM Drivers WHERE FirstName = '$FName' AND LastName = '$LName'");
						$STH->setFetchMode(PDO::FETCH_OBJ);
						while($row = $STH->fetch()) {
						$DriverID = $row->DriverID;
						}

			 		$sql = "UPDATE RacePositions SET FastLap = 'Yes' WHERE RaceID=? AND DriverID = ?";
					$q = $MDB->prepare($sql);
					$q->execute(array($RaceID, $DriverID));
			 		}
		 		}
	        }
	    }
	    fclose($handle);
	}
} else {
     $Msg = 'There was an error during the file upload.  Please try again.'; // It failed .
}
}

Return $Msg;
}

 

That is the code, the CSV is basically like this:

Lewis Hamilton,
Jenson Button,
Romain Grosjean,
Michael Schumacher,
Mark Webber,
Sebastian Vettel,
Nico Rosberg,
Pastor Maldonado,
Nico Hulkenberg,
Daniel Ricciardo,
Jean-Eric Vergne,
Fernando Alonso,
Kamui Kobayashi,
Bruno Senna,
Paul di Resta,
Felipe Massa,
Kimi Raikkonen,
Heikki Kovalainen,
Vitaly Petrov,
Timo Glock,
Charles Pic,
Sergio Perez,
Pedro de la Rosa,
Narain Karthikeyan

 

Thanks for any help!

 

Link to comment
Share on other sites

Before going thruogh all of the code you have posted, the one thing that stands out from your initial comment is that this worked on your dev environment. Have you checked that the file permissions on the new server are correct, and that the file can actually be uploaded / stored / read ?

Link to comment
Share on other sites

Before going thruogh all of the code you have posted, the one thing that stands out from your initial comment is that this worked on your dev environment. Have you checked that the file permissions on the new server are correct, and that the file can actually be uploaded / stored / read ?

 

Thanks for the reply, However I have manage to fix this.

 

It seems it didn't like the CSV file having the Comma's although i'm sure it had them before but it's working now.

 

Thanks for the reply :)

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.