INSERTing text containing commas into MySQL database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

INSERTing text containing commas into MySQL database

Post by mjseaden »

Dear All

For my new site, up until this point I have been using a MySQL database package to set up initial tables and insert test data, whilst developing the database 'reading' parts of the site.

However, I have now come across a problem - whilst I am able to use the MySQL syntax perfectly well, I am unable to insert text into the TEXT fields on the database table I am trying to write to, because it contains a comma and thus MySQL thinks that I am delimiting the fields rather than using English punctuation.

The 'comma' comes from text typed into a text area by the user, which I consequently want to write to the database.

Your help would be very much appreciated.

Many thanks

Mark
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Can you paste the query you are using? Sounds like you arn't quoting values as commas have no special meaning in mysql strings.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

This is a generic form:

Code: Select all

$query = 'INSERT INTO Datatable1 VALUES ('.$myid.', '.$_SESSION['Int_value1'].', '.$_SESSION['Int_value2'].', '.$_SESSION['text1'].', '.$_SESSION['text2'].', '.$_SESSION['float1'].', '.$_SESSION['text3'].', '.$_SESSION['text4'].', '.$_SESSION['int_value3'].', '.$_SESSION['int_value4'].', '.$_SESSION['int_value5'].')';
Regards

Mark
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Yeah, you're not quoting the values. Try :

Code: Select all

$query = "INSERT INTO Datatable1 VALUES (
  '$propid',
  '{$_SESSION['Int_value1']}',
  '{$_SESSION['Int_value2']}',
  '{$_SESSION['text1']}',
  '{$_SESSION['text2']}',
  '{$_SESSION['float1']}',
  '{$_SESSION['text3']}',
  '{$_SESSION['text4']}',
  '{$_SESSION['int_value3']}',
  '{$_SESSION['int_value4']}',
  '{$_SESSION['int_value5']}')";
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Hi mark
Your method seems to work, however only certain fields are being filled. It may be a problem with the $_SESSION variables so I'll have to check, but thanks for correcting the syntax.

Many thanks

Mark
Post Reply