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!
Hi,
When testing this script worked fine.Now I'm a step further and the csv file (actual export form MSAccess) has several fields in it (not all fields).
How can I remove those quotes so the import in MySQL does work ?
<?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 "e;$csv"e;; $csvї0] is the field names, $csvї1] is the first row of data, etc.
$count = count($csv);
$names = explode(';', $csvї0]); // first line.
for ($x = 1; $x<$count; $x++)
{
$q = 'REPLACE INTO testtable SET ';
$fields = explode(';', $csvї$x]);
foreach ($fields as $k=>$col)
{
$q .= $namesї$k].'=\''.$fieldsї$k].'\', ';
}
$q = substr($q, 0, -2); // get rid of the last "e;, "e;.
mysql_query($q);
}
I read all these pages, but still I don't know how to use it for my own purpose.
Still too much newbie, I guess..
How should I write this ?
Appreciate your support..
Hi Burrito,
Thanks for your reply, but it still doesn't import the data.
Here's a few lines from the csv file:
First line are Fieldnames (matching table layout) "Id";"Naam";"Voornaam";"ASNNR";"Datum_lid";"URL";"Vereniging";"Adres";"Postcode";"Woonplaats";"Telnr";"Email";"Oud_Sefnr"
20;"Somelastname";"Hans";6202051;;"http://www.somewebsite.nl/";"Website titel";"Somestreet 12";"4578 AA";"Sometown";"010-124578";"mail@isp.ds";
82;"Nextlastname";"Hans";6105851;;"http://www.otherwebsite.nl/";"blabla";"Otherstreet 1";"7124 BB";"OtherTown";"001-555555";"email@mail.nl";"3000001"
Is there also a possibility to show what has been updated/inserted ?
Now the browserscreen is empty, for future users would be nice to show them what has happened.
Appreciate your help !
Not to be uncontributing to this thread, but I was just reading this down and noticed the Norwegian (or is it Dutch?)... Voorname! reminds me of German (Vorname). Kudos for the scripter for his choice of languages =D
Oke, echo'd ($q) and I think I understand the problem.
For all fields the REPLACE INTO is set instead off INSERT.
Since this testing table is empty there's nothing to replace..
So, if data is not in the table at all, INSERT query should go first.
How do I do that in this PHP script ?
It is Dutch indeed..
(Wow !, I solved a question..LOL)
Sorry for being such a pain, Have I thanked you all for supporting ? THANKS !
Inserting is fine now but...
Other question: Now when a certain Id is in the table, nothing is updated.
Basic idea was to update where needed, and to insert where not in the table.
Anyone feels like adjusting this for me ?
Really appreciate, this is a great forum !
Or you could simply put a unique index on the rows.. And ignore the error that mysql raises when you try to insert a row with an index that already exists..