Solved

import csv to mysql with controle of the same record

Posted on 2011-03-14
6
266 Views
Last Modified: 2012-05-11
Hi
i have a mysql database and one of the tables is called customer,sometimes we have new customers send to us in excel but we change it to csv,and i want to import this list automatically to my database using php
the problems:
sometimes there is the same customer means i have a customer and he can be added to the table i must make a controle,
secondly not all the fields are imported to the database only a few of them
any help will be appreciated
thank you
0
Comment
Question by:asaidi
  • 4
  • 2
6 Comments
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 35129220
A quick Filegetcontents and explode on \r\n will get you each line of the csv. Then explode in commas to get every field. Now you can loop through and build the insert or update queries you need.
0
 

Author Comment

by:asaidi
ID: 35132163
Hi aarontomosky
ok i start to write the script and now when i start to import my csv the script stops because there are names that have (') like o` M` etc
how i can ignore these characters and i used specialchars but did not work for me.
0
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 35132566
i wrap all fields in the mssqles (ms sql escape string) I made below. It trims and escapes quotes and also gets rid of a chr(31) that pissed me off at some point.
function mssqles($string_to_escape) {
	$replaced_string = str_replace("'","''",trim($string_to_escape));
	$replaced_string = str_replace(chr(31),"",trim($replaced_string));
	return $replaced_string;
}

Open in new window

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:asaidi
ID: 35132637
Hi aaron
you know i m a bit new in php
where i will put this function or how i can call it from my script means is it in insert clause or before
thank you
0
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 35133004
I put my stock functions in a file called inc.php
if it's in the same folder, just do:
require_once("inc.php");
at the top of your page.

Then you call the function like in the code below when you build your query:

 
$query = "insert into tablename (column1,column2,column3) values(";
$query .= "'".mssqles($column1)."','".mssqles($column2)."','".mssqles($column3)."');";

Open in new window

0
 
LVL 39

Accepted Solution

by:
Aaron Tomosky earned 500 total points
ID: 35133023
Since you're new to this, I'll also mention that you can do a bunch of rows with one insert statement.
Insert into tablename(column1,column2,column3) values (1,2,3),(4,5,6),(7,8,9)

Damn, I just noticed you are using mysql. Forget my function. Thats for microsoft sql. PHP already has a function built in:
mysql_real_escape_string();
So just replace my mssqles in the example with mysql_real_escape_string
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question