Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy for guys, syntax error

Status
Not open for further replies.

pookie62

Technical User
Oct 22, 2004
139
GB
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:
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);

 
add an echo statement before the query is run to see if there is an issue with the syntax...easiest way to debug the problem...though its likely and extra quote or double where there should be a single quote

Bastien

I wish my computer would do what I want it to do,
instead of what I tell it to do...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top