?
Solved

import csv to mysql with controle of the same record

Posted on 2011-03-14
6
Medium Priority
?
285 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
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 

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

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

801 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