$file = ("UploadedMatch.csv");
$csv = fopen($file , "r"); //this puts each line of the file into it's own element of the array "$csv"; $csv[0] is the field names, $csv[1] is the first row of data, etc.
[red][b]No it doesn't, fopen OPENS the file for reading, the line above this one reads the entire file into an array. You don't need the fopen in this case.[/b][/red]
$count = count($csv); //this is how many rows are in that file
for ($x = 1; $x<$count; $x++) //starting at the first data row, going through all of it
[red][b]You can put the count($csv) into the for statement[/b][/red]
{
list($Id, $Naam, $Voornaam, $ASNNR, $Datum lid, $URL, $Vereniging, $Adres, $Postcode, $Woonplaats, $Telnr, $Email, $Oud Sefnr) = explode("\;",$csv[$x]); //separates the data in the row on tabs into the variables
[b][red]You have spaces in some of your variables. That's why your getting the error. Why are you escaping the semi-colon ";"? Just write it as explode(';',$csv[$x])[/red][/b]
$query = "select Id from deelnemer where Id = $Id"; //this is just to see if that data row already exists in the table
[b][red]Since you're going to updating the record if it exists, use "select *" instead of "select Id".[/red][/b]
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($result))
[red][b]Can you have more than one record in the database with the same Id? If not, why loop through 0 or 1 records? It would be better to do:[/b]
$rc = mysql_count_rows($result);
if ($rc == 1) { // row there, update
$q = 'update deelnemer set ';
$qend = " where Id='" . $Id . "'";
$qtmp = array(); // make sure this is empty
foreach ($row as $columnname => $value) //Note: if you didn't do a "select *" on the original query, you would have only the Id column in the array.
if ($value != $$columnname) // don't really need the double quotes here
$qtmp[] = "$columnname = '$$columnname'";
}
else { // row not there, insert
$q = 'insert INTO testtable'; // the table you're inserting into is different that the table you're retrieving from???
$qend = '';
$qtmp = array();
foreach (array(($Id, $Naam, $Voornaam, $ASNNR, $Datum_lid, $URL, $Vereniging, $Adres, $Postcode, $Woonplaats, $Telnr, $Email, $Oud_Sefnr) as $col) // I replaced the spaces in the columns with underscores "_"
$qtmp[] = $col . "='" . $$col . "'";
}
$q .= implode(',',$qtmp) . $qend;
echo $q . "<br>\n";
[b]Instead of your code.[/b]
[/red]
{
if ($row['Id'])
{
$update = true; //the data row already exists in the table, we need to update it
}
else
{
$update = false; //the data row isn't in the table, we need to insert it
}
}
if ($update)
{
if (is_array($row)){
foreach ($row as $columnname => $value){
if ($value != "$$columnname"){
$update[] = "$columnname = '$$columnname'";
}
}
$updateSQL = "UPDATE deelnemer SET ";
$updateSQL .= implode(',',$update);
$updateSQL .= " WHERE Id = $Id";
# for now, just echo to test
echo $updateSQL;
}
}
if (!$update)
{
$Upquery ="INSERT * INTO testtable values($Id, '$Naam', '$Voornaam', '$ASNNR', '$Datum lid', '$URL', '$Vereniging', '$Adres', '$Postcode', '$Woonplaats', '$Telnr', '$Email', '$Oud Sefnr'";
//execute the query
}