What's wrong ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

What's wrong ?

Post by pookie62 »

Hi,
What is wrong with this code ??
Nothing happens, no update, no insert, no error..
Please review and enhance.. getting bold spots..

Thanks a lot !

Code: Select all

<?php
mysql_select_db($database_ASN, $ASN);
$csv = file("UploadedMatch.csv");  //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.
$count = count($csv);
for ($x = 1; $x<$count; $x++)
{
   list($Id, $Naam, $Voornaam, $ASNNR, $Datum_lid, $URL, $Vereniging, $Adres, $Postcode, $Woonplaats, $Telnr, $Email, $Oud_Sefnr) = 		explode(';',$csv[$x]);

   $query = "select * from testtable where Id = $Id";   //this is just to see if that data row already exists in the table
   $result =mysql_query($query) or die(mysql_error());
   while
  ($row = mysql_fetch_assoc($result))
 {
   $rc = mysql_count_rows($result);
   if ($rc == 1)
   {
      $q = 'update testtable set ';
      $qend = " where Id='" . $Id . "'";
      $qtmp = array(); // make sure this is empty
      foreach ($row as $columnname => $value)
       if ($value != $$columnname)
          $qtmp[] = "$columnname = '$$columnname'";
   }
   else
   {
      $q = 'insert INTO testtable';
      $qend = '';
      $qtmp = array();
      foreach (array($Id, $Naam, $Voornaam, $ASNNR, $Datum_lid, $URL, $Vereniging, $Adres, $Postcode, $Woonplaats, $Telnr, $Email, $Oud_Sefnr) as $col)
      $qtmp[] = $col . "='" . $$col . "'";
   }
   $q .= implode(',',$qtmp) . $qend;
   mysql_query($q);
 }
}
/*
//renaming file
$filename2 = ("UploadedMatch.csv" . '.old');
rename("UploadedMatch.csv", $filename2);

/* Closing connection */
mysql_close($ASN);
?>
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Is this suppose to have 2 dollar signs?

Code: Select all

$qtmp[] = "$columnname = '$$columnname'";
Fixed:

Code: Select all

$qtmp[] = "$columnname = '$columnname'";
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post by pookie62 »

Hi Dale,
Thanks for your reply, I changed it, but still nothing happens..
I found this script somewhere and don't know why there are two $$'s.
The script is supposed to compare the data in the csv file with the table, then update or insert into table.

How would you do this ?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Two dollar signs means it's a reference, and it definitely is supposed to be there.

Two questions though:

1. explode(';',$csv[$x]) .. .csv files usually use comma rather than semi-colon as a seperater. That seems odd. Is it right?

2. Does your database table match the expected columns in this CSV exactly?

Are there really no errors? Have you checked your error log?
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

onion2k wrote:Two dollar signs means it's a reference, and it definitely is supposed to be there.
Ahhh, i'll remember that for the future. Thanks.
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post by pookie62 »

Onion2k,
This is the first line form the csv file:
Id;Naam;Voornaam;ASNNR;Datum_lid;URL;Vereniging;Adres;Postcode;Woonplaats;Telnr;Email;Oud_Sefnr
So, yes the seperator is indeed the semicolon.

This is the structure of the table:

Id bigint(20) Nee auto_increment
Naam varchar(255) Ja NULL
Voornaam varchar(255) Ja NULL
ASNNR bigint(20) Ja NULL
Datum_lid datetime Ja NULL
URL varchar(255) Ja NULL
Vereniging varchar(255) Ja NULL
Adres varchar(255) Ja NULL
Postcode varchar(255) Ja NULL
Woonplaats varchar(255) Ja NULL
Telnr varchar(255) Ja NULL
Email varchar(255) Ja NULL
Oud_Sefnr varchar(255) Ja NULL
Where should I fine the errorlog ??
Hosted website.. so I can't access everything.
Hopes this helps..
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

the error log should be in the same directory where that contains the script

also, make sure that your .CSV file resides in the same directory as well.
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post by pookie62 »

the error log should be in the same directory where that contains the script
There is nothing that looks like a errorlog..
also, make sure that your .CSV file resides in the same directory as well.
Yep, it is
Post Reply