Hi,
I'm using script to update MySQL table from csv file.
I'm getting a syntax error and don't know how to get it right.
Browser error:
right syntax to use near 'lid='',URL='',Vereniging='Somevillage',Adres='The Street 9'
Line from csv file:
2;"lastname";"firstname";0110101;;;"Somevillage";"The Street 9";"1111 AA";"St.Penkris";"001-56717377";"f.lastname@isp.nl";
Anyone wants to add the line for me to get this thing working?? Appreciate it..
This is the script:
I'm using script to update MySQL table from csv file.
I'm getting a syntax error and don't know how to get it right.
Browser error:
right syntax to use near 'lid='',URL='',Vereniging='Somevillage',Adres='The Street 9'
Line from csv file:
2;"lastname";"firstname";0110101;;;"Somevillage";"The Street 9";"1111 AA";"St.Penkris";"001-56717377";"f.lastname@isp.nl";
Anyone wants to add the line for me to get this thing working?? Appreciate it..
This is the script:
Code:
$fp = fopen('Deelnemer.csv', 'r');
// first line has column names
$data = fgetcsv($fp, 2048, ';');
$columns = array();
foreach($data as $column)
{
$columns[] = trim($column, '"');
}
$sql = 'INSERT INTO testdeelnemer (';
$sql .= implode($columns, ', ');
$sql .= ') VALUES (';
// next lines have values
while (($data = fgetcsv($fp, 2048, ';')) !== FALSE)
{
$checksql = "SELECT id FROM testdeelnemer WHERE id=".$data[0];
$result = mysql_query($checksql);
$row = mysql_fetch_row ($result);
if ($row[0])
{
$sql2 = "UPDATE testdeelnemer SET ";
$sql_clause=array();
foreach ($data as $key=>$column)
{
if ($key != 0)
{
$sql_clause[] = $columns[$key]."='".mysql_real_escape_string($column)."'";
}
}
$sql2 .= implode (",",$sql_clause);
$sql2 .= " WHERE ".$columns[0]." = '".mysql_real_escape_string($data[0])."'";
} else
{
$sql2 = $sql;
foreach($data as $column)
{
$column = mysql_real_escape_string($column);
$sql2 .= "'{$column}', ";
}
$sql2 = rtrim($sql2, ', ');
$sql2 .= ')';
echo 'Executing: ' . $sql2 . '</br>';
}
mysql_query($sql2) or print(mysql_error() . '<br>');
}
fclose($fp);