473,385 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

apostrophe breaking an insert query from import csv file

170 100+
Hi all,

I have the code:

Expand|Select|Wrap|Line Numbers
  1. if(is_uploaded_file($_FILES['file']['tmp_name'])) 
  2.     {
  3.     move_uploaded_file($_FILES['file']['tmp_name'],$_FILES['file']['name']);
  4.     }
  5.  
  6. $fcontents = file ('students.csv'); 
  7. $size=sizeof($fcontents);
  8. for($i=0; $i<sizeof($fcontents); $i++) 
  9.     { 
  10.     $line = trim($fcontents[$i]); 
  11.     $arr = explode(",", $line); 
  12.  
  13.     $sql = "insert into staffcba_students values ('','$user','$pass','$classyear','$classname','$classid','".implode("','",$arr)."')"; 
  14.     mysql_query($sql);
  15.  
  16.     if(mysql_error()) 
  17.         {
  18.         echo mysql_error() ."<br>\n";
  19.               } 
  20.     }
  21.  
The csv file looks like

Peter,Porridge,M
Ethel,Elephant,F

All works perfectly until one of the names has an apostrophe

Eg.
Peter,O'Toole,M

Now I can see why it's breaking it. But I'm not sure how to fix the problem.

I tried addslashes around the trim($fcontents[$i]), but to no avail. Can anyone suggest a simple solution?
Dec 5 '07 #1
1 2781
stepterr
157 100+
Take a look at the get_magic_quotes_gpc function. I think that may solve your problem. Link
Dec 15 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Michael Sterling | last post by:
i'm using delphi 7 and have a query in which i'm trying to find names that have an apostrophe in them, i.e. "o'mally". my problem is that when i write my select statement i can't get the quotes...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
13
by: Richard Hollenbeck | last post by:
To prevent future apostrophe bugs and errors, isn't it just simpler to forbid an apostrophe from being entered into a text field? For example, couldn't "Alice's Restaurant" be changed to "Alices...
1
by: congngo | last post by:
Hi all Every time I export a table into an excel spreadsheet. It has a leading apostrophe on every cells. This drive me nut. I have to do a work around by export table into a txt file than...
9
by: dajava | last post by:
Hi, Sorry for this beginner's question. I do not know PHP and write for my friend. He has never been a professional programmer. He studied C and PHP with some books and made a bulletin...
12
by: Johnny BeGood | last post by:
Hi All, When a user enters an Apostrophe into a text area field on a form, i.e. didn't, it mucks with odbc as follows Syntax error (missing operator) in query expression ''didn't', Whats...
8
by: SaltyBoat | last post by:
Needing to import and parse data from a large PDF file into an Access 2002 table: I start by converted the PDF file to a html file. Then I read this html text file, line by line, into a table...
2
by: vijaykumardahiya | last post by:
Hello Sir, I have a simple Issue but It is not resolve by me i.e input parameter are not store in Ms-Access. I store the input parameter through Standard Action <jsp:useBean>. jsp:useBean call a...
1
by: ArizonaJohn | last post by:
Hello, The query below works if $find is just a regular word with no special characters, and it even works if $find has a "%" in it or a "&". However, it does not work if $find has an...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.