Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

import csv to mysql with controle of the same record

Posted on 2011-03-14
6
Medium Priority
?
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

636 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