Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    PHP




Duplicate Records In DB When Importing From CSV File


The code below is used to grab each row from a CSV file (except the
first row) and insert it into a database, as long as it's not already
in the DB.

If I echo each row's data, it appears only once on the page. But, for
some reason it inserts each row into the database 3 times. The code
that echos the data, and the DB insertion code all appear within the
same for loop.

The database insertion code (below) works perfectly in another script.
It's just copied/pasted into the for loop.

$row = 0;
$handle = fopen ("some-file.csv","r");
while ($data = fgetcsv ($handle, 1000, ",")) {
if($row == 0){
// set the varible array
$num = count ($data);
$csvNames = array();
for ($c=0; $c < $num; $c++) {
$csvNames[$c] = $data[$c];
eval("$" . $data[$c] . " = array();");
}
$row = 1;
}else{
$num = count ($data);
$row++;
for ($c=0; $c < $num; $c++) {
$buildEval = "$" . $csvNames[$c] . " = "" . $data[$c] .
"";";
eval($buildEval);
echo $buildEval . "<br>"; // this properly displays only one
row's data from the CSV file, then displays the next row in the CSV
file when it loops again.

// BUT FOR SOME REASON, THE DB CODE BELOW INSERTS EACH ROW FROM THE
CSV FILE 3 TIMES.

// Set variables with current date and time to be inserted into DB
later
$appDate = date('Y-m-d');
$appTime = date('H:i:s');

// Prepare the phone numbers for easy input into the database further
down this page.
$fHomePhoneCombined = $fHomePhone1 . "-" . $fHomePhone2 . "-" .
$fHomePhone3;
$fBusinessPhoneCombined = $fBusinessPhone1 . "-" . $fBusinessPhone2 .
"-" . $fBusinessPhone3;

// Connect to the database
$db = mysql_connect ("localhost", "username", "password");
mysql_select_db ("mydatabase");

// Check to see if the record is new,
$newRecord = 1;
$queryNewRecord = mysql_query("SELECT * FROM cust_test WHERE
first_name = '$fFirstName' AND last_name = '$fLastName' AND email =
'$fEmail'");
if (mysql_num_rows($queryNewRecord) > 0){
$newRecord = 0;
}

// If it's NOT a new record, insert the contents into cust_test_bad DB
// I'll insert this code later. It's not important for this test.

// Write the information to the customers table, if it's a new record.

if ($newRecord == 1){
$query = "INSERT INTO cust_test SET
application_date = '$appDate',
application_time = '$appTime',
first_name = '$fFirstName',
last_name = '$fLastName',
email = '$fEmail',
address = '$fAddress',
state = '$fState',
zip = '$fZip',
home_phone = '$fHomePhoneCombined',
best_time_to_call = '$fBestTime',
lead_buyer = '$lbuy'";


$result = mysql_query($query) or die('Failed because:
'.mysql_error());
}

} // end of for ($c=0; $c < $num; $c++)
}
}
fclose ($handle);




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Print Non Duplicate Records
I want to search a table and display the records which are not duplicated in a different field of the same table. For instance I can print the duplicate records with this piece of code:

Prevent Adding Duplicate Records
theres a XML file located on the internet. every week or so. this xml file gets updated with one or two new  entry's.

i have a php script which can read all the entrys i want from this xml file. and then put it into a database.

the problem is say i run the php script again to update the database and add the new entry's that the xml file has. every thing gets added again. each time the script is run. the enter xml file entry's are added again.
so the database get full with duplicates triplcate records ect ect.

how would i go about preventing the same record being added over and over.

Importing Csv Text File Into MySQL
I have a CSV file delimited by ~. Here is an example of 3 records: Code:

Importing Text File Into MySQL DB
I'm writing a script to import a text file with fields delimited by ~ into a MySQL DB. I'm trying to import everything into an array first, so that one array row = one record row. Here is some dummy data, with the &#390170;' record having a couple of new lines where a new paragraph is. Note the other records have NULL in the same field: PHP Code:

Importing To DB From Pipe Delimited Text File
I am trying to make a PHP script that will import data from a pipe delimited text file in to a database. The problem I am having is that the fields have long descriptions in them and there are multiple line breaks for paragraphs. They contain HTML codes as well (<B>).

The multiple line breaks are causing it to import the data strange, putting data in the wrong fields and totally missing some data as well. Here is what the text file Code:

Importing Excel File Into Mysql Database
I need help regarding importing excel file into mysql database.I am using PHP 5.0.2, mysql 4.2.13. I need code for importing excel file into mysql database and what will be the steps for it.

Importing Large Amounts Of Data Via File Upload
I was wondering how anyone here deals with move large amounts of
mysql data to be uploaded to the server? Large as in 75MB CSV file. I
have a client who wants to admin there database with 15,000 users.
They manage all data offline and want to upload to the site for
display purposes only. My server has 5MB php file upload limit. I
really do not want to up this limit.

What I am wondering is how does cPanel or PHP MyAdmin get around this
upload limit using php scripting?

Importing .sql File Replaces Special Characters (á, ç Etc.) With Questions Marks
I am trying to import an .sql file into a new instalation, but the resultant table replaces special characters (á, ç etc.) with questions marks.

Editing entries manually via PhpMyAdmin doesn't work either.

Destination versions:
phpMyAdmin 2.6.1
MySQL 4.1.9-max

Importing A Flat Pipe Delimited File Into A MySQL Data Table
I have done this in perl but eeew I don't want to use perl anymore. I
want to be able to take a flat file that looks like this:

mbriones@...|Marian|Briones
bobsmith@...|Bob|Smith
janedoe@...|Jane|Doe
(etc)

The table has more than 3 fields, so I'd want to process the flat file
and then do an INSERT into mailinglist SET field=$value for each line.

Does someone have a nice way for me to do this? The flat file will be
uploaded to the server via a form (copy) and then be processed.

Remove Duplicate Entries From TXT File ?
Is it possible to use PHP to open/read a TXT file (i.e. IP.TXT) that
contains ip addresses (1 per line), remove any duplicates found, and
re-write the file back out to IP_NEW.TXT ?

The reason is, I have ips coming from several sources, i.e. URLSCAN logs,
MySQL dbs, etc. and I can use a DISTINCT on the individual queries, but I
then combine all the results into 1 file which may contain duplicates.

Writing Records To File Instead Of A DB
Is there a way to write records and keep them organized to a file instead of a DB?

Writing Records To Text File
I have the following code which displays some some formatted text in my
browser. The problem is I need to create a text file and write the records
from this array into it. Does anyone know how to acheive this...

Displaying Records From Text File
I have the following Code which reads in information from a text file: PHP Code:

Displaying Priorty Records And Normal Records In The One Dataset.
I have a page which displays the records from the database and uses paging to display the records in groupes of 10 per page.

I now want to be able to show results depending on the database field (display) in the database (can be set to either "full" or "limited"). If the record and the field display is equal to "full" i want to then display this record at the top of the record set and display all the fields.

If the record is equal to "limited" i want to display different results and they must show after all the records which are equal to "full". I can't output two different recordsets as i use paging and only want to show 10 records per page. Code:

Importing PDF Or EPS
Does anyone know of a way to import an existing PDF or EPS for editing without the commercial PDI library?

Importing Data
I have this file cvs that's roughly 2.5 meg. Right now, it uses a function like this to import the data into the table. PHP Code:

Importing Pages
I've written a content management system that I'm now selling to my
customers. It's very nice when we have a blank canvas of a site, but a pain
in the arse when there is already a site in place.

What I'm in the process of *trying* to put together is a script that would
do the following:

A simple form where you put the address of the site with the static pages

The script then spiders through the site, takes everything between <body>
and </body> and chucks the rest away

It would then take out all class definitions and all embedded styles like
font tags etc but leaves tables, <p> <H?> etc

This would leave a very plain page of HTML that would be inserted into a
database. CSS would control the fonts etc. I'm aware that there would need
to be some tidying up if there was any javascript or anything and also some
basic formatting.

What I want to know is

1. Has it been done and, if so, where might I find something like this
2. Might it have any commercial value to other developers?

Regarding 2, I'm thinking how much time something like this might save me if
I have to convert anything more than a few pages of static HTML into
something that I can put in a database.



Importing SQL Files
is it possile to import data from a sql file which has been exported from another database without using something like phpmyadmin ?  Or must it be done using phpmyadmin.

Also is it possible to just export data without having to use phpmyadmin or similar?

CSV Importing To MySQL
Well I had the last script working perfectly. Except sometimes we rearrange the CSV "headers" so a field may be in one place on one file and another place on another file, yet they are the same thing. I've tired about 30 different scripts and none of them work at all. For example:

Csv file looks like:
work order,client name,start date,end date
7283728,"name,client",02/07/07,04/02/07

that is just the test file I've made up. I found out, the hard way, you cannot have spaces in your database fields. So what I am trying to do is take that first line, replace all spaces with an _. That away database fields can be like work_order and in the CSV file also. I tried using Code:

Importing Xml To Mysql
i am using this piece of code to import an xml file to mysql.

$feed[$key] = $info;

is causing the script to only return the last element of the xml file.

I would like to return all elements but cant find a way to do so,

<?php
    include 'library/config.php'
    include 'library/opendb.php'
$file = "my-xml.xml";

$feed = array();
$key = "";
$info = "";


function startElement($xml_parser,  $attrs ) {
  global $feed;  
   }

function endElement($xml_parser, $name) {
  global $feed,  $info;
   $key = $name;
  $feed[$key] = $info;
  $info = ""; }

function charData($xml_parser, $data ) {
  global $info;
  
  
  $info .= $data; }
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "charData" );
$fp = fopen($file, "r");
while ($data = fread($fp, 8192))
!xml_parse($xml_parser, $data, feof($fp));
xml_parser_free($xml_parser);

$sql= "INSERT INTO section ( ";
$j=0;
$i=count($feed);

foreach( $feed as $assoc_index => $value )
  {
  $j++;
  $sql.= strtolower($assoc_index);
  if($i>$j) $sql.= " , ";
  if($i<=$j) {$sql.= " ) VALUES ('";}
  }
$h=0;
foreach( $feed as $assoc_index => $value )
  {
  $h++;
  $sql.= utf8_decode(trim(addslashes($value)));
  if($i-1>$h) $sql.= "', '";
  if($i<=$h) $sql.= "','');";
  }
  $query=trim($sql);
  echo $value;
if (mysql_query($sql)){
echo "success in table creation.";
} else {
echo "no table created.";
}
echo mysql_error();
  echo $sql;
?>

Importing Data Into Mysql
I'm starting working on a new project where I'll have to allow users to upload and import tab-formatted text files into the db. Should I use the LOAD DATA INFILE from PHP or write a custom procedure?

Importing From CSV Using LOAD DATA
what attributes should i use for a text coloum should it be binary?

Importing Data From Excel Into PHP
Is it possible to import data from excel file into php and than put it in
mysql database?

Import_request_variables Isn't Importing My Changes To My Globals
The server I'm running my PHP on has "magic_quotes" turned on, so all
the input I get (from GET, POST, and cookies) is escaped with
backslashes. I don't want this (I handle my own escaping, and I need to
be able to work with unescaped strings), but I'm not allowed to change
the setting on the server, so I need to figure out a way to "undo"
magic quotes.

This code itself works...

function stripslashes_array($v) {
return is_array($v) ? array_map('stripslashes_array', $v) :
stripslashes($v);
}
if (get_magic_quotes_gpc()) {
foreach (array('POST', 'GET', 'REQUEST', 'COOKIE') as $gpc)
$GLOBALS["_$gpc"] = array_map('stripslashes_array',
$GLOBALS["_$gpc"]);
}

But my problem is that I then want to use import_request_variables to
bring any form data into the global variable space, and that's not
reflecting the changes I just made to the globals... in other words, if
after that code I say:

$foo = "GLOBALS["_GET"]["test"];
import_request_variables("gp", "in_");
$bar = $in_test;

and I submit the page with "test" set to a'b'c, then $foo will be a'b'c
while $bar will be a'b'c.

How do I get the stripslashed values into my global variables without
having to stripslash each one individually?

Importing With PhpMyAdmin Error.
I've been testing import csv files with PHP. Well it's going ok, I guess, when you save with Excel everything works fine. The output .csv file is from another piece of software, and it appears to be completely fine. Look at this:

Line [1] from Excel saved CSV under "date" is this.
59:04.0

Line [1] from Software saved CSV under "date" is this.
2007-06-14 11:59:04.0

Excel takes out most of the date/time. When it is almost taken out it imports with PhpMyAdmin just fine. Well I need that date. For some reason when that date/time is complete MySQL kicks it back. Know the weird thing, I can go into PhpMyAdmin and insert just "2007-06-14 11:59:04.0" into the "date" column. So, this completely makes no sence why MySql is kicking it back. When I compare both csv files they are exactly alike, except the date; yet, there is no problem...anyone have any idea how to fix this? Or maybe a loop around it using PHP. I've got a class file that imports the Excel CSV just fine.

Using COM Objects Or Importing To Outlook
I've been asked to look at developing a web-based system for tracking forms.  In itself, it is fairly simple.  However, one item they want to look at is Outlook integration for tasks.  We use an internal exchange server and I have read about being able to use COM objects to write tasks for Outlook from PHP.

However, I'm having difficulty with this part. The script I do have is timing out and documentation on using COM objects is fairly sparse. Should I be looking at something other than PHP? Code:

Importing Excel Into A MySQL Database
i have an excel file I want to read into a database.  I found a tutorial which says to save the file into a xml file, then it basically gets all the data out, and puts it into an array Code:

Importing Data To MySql From Web Page
I'm using Drupal(a CMS), along with MySQL and PHP to import data into a database.  I'm trying to import a bunch of sentences on one page into the database, but each in their own row.  For example:

News
-----
Brand new site
New employee hired
New work acquired

Basically, I need to know how to pull those into the database as one row each, instead of pulling all three elements into the database in the same row.  They're all stored in a $content tag, b/c I wrote the page in PHP.  So, any ideas on how to make it, so I can insert the $content into the database, but at a line break, I need to insert a new row?

Importing Form Data To Ms Access
Does anyone know if one can import a web form data into ms access db? Does the ms access db have to be on the same server as the web form?

Encoding Error When Importing With Simple XML
When importing XML with SimpleXML -php5-with accents they come with errors... I guest this is an encoding thing... example:  when importing this frase in spanish "La Tecnología más avanzada" I get:

La Tecnología más avanzada

if the original text was in utf-8 or in iso-8859-1 how do I can get them correctly according to it encoding?

Importing To Mysql Without LOAD DATA LOCAL
I've just gone through the learning curve of realizing most virtual hosts have "LOAD DATA LOCAL" disabled :(

I have a two meg delimited ascii file I need to import on a regular basis (26 fields)

Anyone have a nice import routine using fopen they are willing to share before I attempt to hack one out myself? I can't seem to find such an animal out there (though some are close).

Such a shame, from what I have read, load data is much faster...

Importing A Field From MySQL Into A Variable In Flash?
I have a query that is giving me the field that I require from my database using PHP.
What is the best way to import this variable into flash? Can I echo the number out on the page and have Flash import it that way? Could I use the Load Variable command? code:

Importing Items From Online Calendar To Mail Client
Has anyone any idea if it's possible to import items from a database table into your mail client calendar? I'm using Ximian Evolution, but Outlook is very relevant to, because most users are still stuck in the M$ mess...

ON DUPLICATE KEY
I have a query where I'm trying to insert data using the ON DUPLICATE KEY syntax.

When I run the script, I receive the following message:
FS30YF100FS30YF100Unknown column 'a' in 'field list'. PHP Code:

Duplicate Entries
I am currently using the form below for people to enter data into mysql, at the moment this has no methed of checking to see if the data they enter already exists, can anyone provide help as to how to this? PHP Code:

Prevent Duplicate
I have this code that inserts the email address into a database but I want to check the database to see if that email address already exists and if so tell the user. I've looked all over to find out how to do this. PHP Code:

Duplicate SQL Entries
I have a buddy system on my site and I have found that duplicate buddies can be added into the database. Here how I have this set up:

user 1 want to add user 2 as a buddy.
user 1 goes to user 2's page and clicks "add as buddy"
A PM goes to user 2 and asks him if he accepts or declines
if user 2 clicks accept a php script is run

Problem:
This script need to detect if that buddy already exists, if so do nothing or overwrite the original. Right now it just adds the entry again. I am using a very simple table:

UID   BID
---------
1   |   2

This would mean user id 1 has a buddy with a buddy id 2

Here is the non-functional code:

$query = mysql_query("UPDATE buddies SET uid='$uid', bid='$bid' WHERE uid='$uid' && bid='$bid'");
print "Buddy Has been overwritten";
//would prefer it not do anything at all if entry exists
if ($query == 0)
{
// No records updated, so add it
$query = mysql_query("INSERT INTO buddies (uid, bid) VALUES ('$uid', '$bid')");
print "Buddy Added";}

Duplicate Enterys
What is the best way to stop duiplicate enterys on to a data base from a website, for instance if the user presses refresh, or presses back and then submits again.

up to yet i have been using a check in the database for the same info being added, and using an if statment..

Duplicate Data
i am doing a simple program to let user entry their travelling claim. how can i notify user when there are duplicate data (they enter same data twice) ?

Duplicate Entry
this functions adds new locations in am_ups_shipment_location table after a tag in XML is detected it is pointed towards this function and this function then goes and adds the new location.

for the first time when i run the php prgoram the new location gets added.but when i refresh ,again the same location gets added but with different id[id is on autoincrement].

here is how my table looks after refresh.[you can see duplicate entry].This is what i dont wnat...i dont want a duplicate entry.
Can you please help me get rid of this duplicate entry by writing some update functions and checking against already present data in the table? Code:

Duplicate Results
I'm fairly new to PHP and after some trial and error managed to get the rsults I want from the following script, except that now some of the results are being duplicated. Code:

Checking For Duplicate Users
Can anyone point me in the right direction? I'm looking for help in checking my user table for existing names and passwords before i add a new user.

Insert On Duplicate Key Update
I have the following code: PHP Code:

Trying To Duplicate Form Fields.
What I would like is to when someone writes in one field. The same text is written in a seperate field. But without sending anything. all internally. Anyone have a clue?

Duplicate Emails From Formmail.php
Can anyone tell me why I might be getting 2 email returns for every form post using formmail.php?

Both are identical, both have the same time sent.

I'm using the recipient in the formmail.php script because when I use recipient in my form, it doesn't work.

Selecting Duplicate Entries
I'm having the following problem: Let's assume I have a table with 3 fields: ID (type int), NAME(type varchar), EMAIL (type varchar) in a mysql db. The fields have the following data stored:

1, jack, jack@jack.com
2, tom, jack@jack.com
3, jack, jack@jack.com
.
2000, jack, jack@jack.com

Now let's say I have like some thousand entries like that one above with each one containing different data. So if there are entries which have the same email adress, how I can I select exclusively those entries with the duplicate data in it? So the result of a selection should look something like this: (If I want to show the selection results in html): PHP Code:

Ignoring Duplicate Entries
I have this samll piece of code that gets the last 5 post from a forum and presents them on the home page. But i am not sure how to prevent duplicate entries from appearing on it. PHP Code:

While Producing Duplicate Results
I'm trying to add the ability to attach categories and subcategories attributes to each post. I've created two tables (chap_category, and chap_sub_category) that stores the names and a unique id number for each.

It's suppose to list the Categories that corresponds to the forum I'm adding a new post to. Under each of the Categories, its corresponding Sub-categories will be listed. But now it's listing the proper sub categories for the first category, for the second category it lists the first category's sub categories, for the third category it lists the first's sub-categories AND the second's subcategories...etc. PHP Code:

Duplicate Elements In Array
is there an efficient way to check if all the entries in an array are unique or if there are duplicates?


Copyright © 2005-08 www.BigResource.com, All rights reserved