Solved

import csv to mysql with controle of the same record

Posted on 2011-03-14
6
272 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
Independent Software Vendors: 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 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Link failure 16 63
Multi line FPDF footer: 3 24
CSRF session and form tokens never match when using php/AJAX 4 28
Group By Function Required in PDF Output 21 33
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
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 …

756 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