Page 2 of 2

Posted: Sat Sep 10, 2005 12:53 pm
by feyd
I don't see anything, so it loooks GREAT! :lol:

Posted: Sat Sep 10, 2005 1:09 pm
by pookie62
Well that's GREAT news indeed !
Thanks a lot !!!

Posted: Sat Sep 10, 2005 1:10 pm
by feyd
maybe you didn't catch it... I literally don't see any code if you intended to post it with your previous post...

Posted: Sun Sep 11, 2005 4:34 am
by pookie62
Woehaa... :lol:
A new page started when you said that it looked great, didn't check my post...

Oke, here is the code as it is now, please have a look..

Code: Select all

<?php
mysql_select_db($database_ASN, $ASN);
if (!file_exists('Score.csv'))
{
die ("Er is geen Score bestand aangetroffen, voer eerst de Upload uit vanaf het Website menu !");
}
$fp = fopen('Score.csv', 'r');
 
// first line has column names 
$data = fgetcsv($fp, 2048, ';'); 
$columns = array(); 
foreach($data as $column) 
$columns[] = trim($column, '"'); 

$sql = 'INSERT INTO testscore ('; 
$sql .= implode($columns, ', '); 
$sql .= ') VALUES ('; 

// next lines have values 
while (($data = fgetcsv($fp, 2048, ';')) !== FALSE) 
{ 
  $checksql = "SELECT Id FROM testscore WHERE Id='".$data[0]."'"; 
  $result = mysql_query($checksql) or die(mysql_error()); 
  $row = mysql_fetch_row ($result); 
  if ($row[0]) 
  { 
     $sql2 = "UPDATE testscore SET "; 
     $sql_clause=array(); 
     foreach ($data as $key=>$column) 
     {    
        if ($key != 0) 
        { 
             $sql2 .= $columns[$key]."='".mysql_real_escape_string($column)."',"; 
        } 
     } 
     $sql2 = rtrim($sql2, ","); 
     $sql2 .= " WHERE ".$columns[0]." = '".mysql_real_escape_string($data[0])."'"; 

  } else 
  { 
     $sql2 = $sql; 
     foreach($data as $column) 
     { 
       $column = mysql_real_escape_string($column); 
	   $column= preg_replace('#^((?:(?:(?:[0-9]{1,3}\.(?:[0-9]{3}\.)*)(?:[0-9]{3}))|0|[0-9]*))(,[0-9]+)?$#e','str_replace(\'.\',\'\',\'\\1\').str_replace(\',\',\'.\',\'\\2\')',trim($column, '"')); 
       $sql2 .= "'{$column}', "; 
     } 
     $sql2 = rtrim($sql2, ', '); 
     $sql2 .= ')'; 
     echo 'Executing: ' . $sql2 . '</br>'; 
  } 
  mysql_query($sql2) or print(mysql_error() . '<br>'); 
  } 
fclose($fp);

Posted: Sun Sep 11, 2005 7:39 am
by feyd
your implode() arguments are in reverse order. ;) And you really should do a mysql_free_result() on the select you do after using it. Not doing so could make your MySQL server spiral out of control on memory usage. :)

Posted: Sun Sep 11, 2005 8:42 am
by pookie62
Can you show me how the code should look then ?
I don't have the skills..
Thanks !

Posted: Sun Sep 11, 2005 8:46 am
by feyd
place

Code: Select all

mysql_free_result($result);
after

Code: Select all

$row = mysql_fetch_row ($result);

Posted: Sun Sep 11, 2005 10:08 am
by pookie62
Thank you so much Feyd !